Sunday, 27 August 2017

jquery - How to detect document.ready in JavaScript?











I have a framework-less javascript that executes on load:




function myJs() {    
// some code
}
window.load = myJs;


But this causes a delay in the execution of the script. I want to be able to execute this script as soon as the document is ready to be manipulated even before the page has completely finished loading. How can this be done in a cross browser compatible way? In other words how can the:



$(document).ready(function() {  

//
});


of jQuery be said in plain JS?


Answer



I have made an implementation (based on the one found in jQuery) that you can use: http://github.com/jakobmattsson/onDomReady/blob/master/ondomready.js


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...