Don't have a window.onload yet?

I have a script that will be inserted on an unknown page at an unknown time. Can one tell from this script if window.onload is already running?

I have no control over the main page. I cannot add markup or scripts to it. All I know is that my script will be inserted into the page at some point. It can be before window.onload or after; and I need to determine which side of this event I am on.

+6
javascript domready
source share
2 answers

Updated answer: Look at this site. It uses the trick, seeing that the last element of document.getElementsByTagName ("*") is defined or not. It seems to work in Opera and IE.


Original answer: You cannot check, but you can do:

window.setTimeout(function () { // do your stuff }, 0); 

This will do your things definitely after loading the page.

+1
source share

jQuery ready() will queue a function that will be launched when the document is loaded, or immediately start fire if the document is already loaded:

 $(document).ready(function() { //your code here }); 

Just make sure jQuery is included before your script block. If you cannot directly include any script resources, you can always copy the jQuery-min body into your script.

-one
source share

All Articles