javascript tutorial - [Solved-5 Solutions] Pure javascript equivalent to jQuery’s $.ready()
- javascript - java script - javascript array
Problem:
However, let's say We want to run a function that is written in standard JavaScript with no library backing it, and that We want to launch a function as soon as the page is ready to handle it. What's the proper way to approach this?
We know We can do:
We can use the body tag:
...or We can even try at the bottom of the page after everything but the end body or html tag like:
What is a cross-browser(old/new)-compliant method of issuing one or more functions in a manner like jQuery's $.ready()?
Solution 1:
The simplest thing to do in the absence of a framework that does all the cross-browser compatibility for we is to just put a call to our code at the end of the body. This is faster to execute than an onload handler because this waits only for the DOM to be ready, not for all images to load. And, this works in every browser.
If we really don't want to do it this way and we need cross browser compatibility and we don't want to wait for window.onload, then we probably should go look at how a framework like jQuery implements it's $(document).ready() method. It's fairly involved depending upon the capabilities of the browser.
To give we a little idea what jQuery does (which will work wherever the script tag is placed).
If supported, it tries the standard:
with a fallback to:
or for older versions of IE, it uses:
with a fallback to:
And, there are some work-arounds in the IE code path that We don't quite follow, but it looks like it has something to do with frames.
Here is a full substitute for jQuery's .ready() written in plain javascript:
The latest version of the code is shared publicly on GitHub at
Usage:
This has been tested in:
Solution 2:
We would like to mention some of the possible ways here together with a pure javascript trick which works across all browsers:
The trick here, as explained by the original author , is that we are checking the document.readyState property. If it contains the string in (as in uninitialized and loading, the first two DOM ready states out of 5) we set a timeout and check again. Otherwise, we execute the passed function.
Solution 3:
If we are doing VANILLA plain Javascript without jQuery, then we must use (IE9+)
Above is the equivalent of Jquery .ready:
Which ALSO could be written SHORTHAND like this, which jQuery will run after the ready even occurs.
NOT TO BE CONFUSED with BELOW (which is not meant to be DOM ready)
DO NOT use an IFFWE like this that is self executing : example:
This IFFWE will NOT wait for our DOM to load. ( I'm even talking about latest version of Chrome browser!
Solution 4:
The good folks at HubSpot have a resource where we can find pure Javascript methodologies for achieving a lot of jQuery goodness - including ready
example inline usage:
Solution 5:
Our method (placing script before the closing body tag)
is a reliable way to support old and new browsers.