Has window.onload fired yet?
Asked Answered
M

2

6

I have a script that will be inserted into an unknown page at an unknown time. Is it possible to tell from this script if window.onload has fired yet?

I have no control over the host page. I can't add any markup or scripts to it. All I know is that my script will be inserted into the page at some point. It could be before window.onload or after; and I need to detect which side of that event I'm on.

Misadventure answered 6/10, 2009 at 2:41 Comment(2)
I find the "unknown time" part curious. Is this for a bookmarklet? Dynamic <script> inclusion? Seems weird to not know the basic use case for inclusion of your own script...Recommend
It's a dynamic script inclusion. I simply can't determine if it happens before or after window.onload.Misadventure
U
1

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


Original Answer: You can't check, but you can do:

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

That will do your stuff definitely after the page has loaded.

Underbodice answered 6/10, 2009 at 21:4 Comment(3)
That will run the function before the DOM is ready in Opera, and may error out in IE. See remysharp.com/demo/onload.php for the IE problem.Recommend
Nice find. I want to run that solution through the laundry, see how well it works.Recommend
As explained in the answer, I don't know of a real answer to the OP's question. However, his situation might be helped by my answer, depending on whether the difference between window.onload and DOM ready is significant.Underbodice
H
-1

jQuery's ready() will queue a function to be fired when the document is loaded, or fire immediately if the document is already loaded:

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

Just be sure jQuery is included before your script block. If you can't include any script resources directly, you can always copy the body of jQuery-min in your script itself.

Haystack answered 6/10, 2009 at 2:55 Comment(5)
jQuery as of 1.3.2 cannot detect if the document is loaded if it is itself included after the document has loaded (e.g. bookmarklet, dynamic <script> inclusion, etc), in non-IE browsers. In such cases the function passed to .ready() will never be called. I'm not sure if the OP is inquiring about such a use-case but just thought I'd mention it.Recommend
jQuery's ready function doesn't appear to work unless it is loaded before window.onload.Misadventure
@Diet: in IE it works, as I mentioned. Anyway I take it from your comment that the code you are writing needs to work if included via a dynamic <script> tag injected into the DOM? Please clarify.Recommend
OK so here's the bug report for what I described: dev.jquery.com/ticket/4196 Nothing has happened with it for 8 months :(Recommend
The original post asks about window.onload, not DOM ready. -1Kakaaba

© 2022 - 2024 — McMap. All rights reserved.