tags:

views:

2152

answers:

5

I need to execute some javascript when the page has fully loaded. This includes things like images.

I know you can check if the DOM is ready, but I didn't know if this is the same as when the page is fully loaded.

+6  A: 

That's called onload. It came waaaaay before DOM ready was around, and DOM ready was actually created for the exact reason that onload waited on images.

window.onload = function () { alert("It's loaded!") }
Matchu
+1  A: 

the window.onload event will fire when everything is loaded, including images etc.

You would want to check the DOM ready status if you wanted your js code to execute as early as possible, but you still need to access DOM elements.

codefly
+1  A: 

You may want to use window.onload, as the docs indicate that it's not fired until both the DOM is ready and ALL of the other assets in the page (images, etc.) are loaded.

Marc Novakowski
Ah brilliant. Well ive done this. The last image on the page is this:<img src="imdbupdate.php?update=ajax" style="display:hidden" onload="window.location = 'movies.php?do=admincp'" />However it doesn't readirect me
Ben Shelock
...what? You want to redirect once an image loads?
Matchu
I'm not sure why you'd want that, but the reason it doesn't redirect may be that it's display:hidden, and thus the browser doesn't bother to load it since it will not be shown.
Matchu
maybe it's just a hidden image that does something important on the server, such as tracking a hit or doing something in the DB?
Marc Novakowski
I can get that, but to have it redirect after doesn't make sense to me. Why have the page at all if you don't want the user to spend time on it? And if the goal is to redirect on load, why not just use a window.onload event, since it's a cleaner implementation overall?
Matchu
Yeah, good point
Marc Novakowski
+1  A: 

And here's a way to do it with PrototypeJS:

Event.observe(window, 'load', function(event) {
    // Do stuff
});
Mark Biek
+2  A: 

Usually you can use window.onload, but you may notice that recent browsers don't fire window.onload when you use the back/forward history buttons.

Some people suggest weird contortions to work around this problem, but really if you just make a window.onunload handler (even one that doesn't do anything), this caching behavior will be disabled in all browsers. The MDC documents this "feature" pretty well, but for some reason there are still people using setInterval and other weird hacks.

Some versions of Opera have a bug that can be worked around by adding the following somewhere in your page:

<script>history.navigationMode = 'compatible';</script>

If you're just trying to get a javascript function called once per-view (and not necessarily after the DOM is finished loading), you can do something like this:

<img src="javascript:location.href='javascript:yourFunction();';">

For example, I use this trick to preload a very large file into the cache on a loading screen:

<img src="bigfile"
onload="this.location.href='javascript:location.href=\'javascript:doredir();\';';doredir();">
geocar
http://en.wikipedia.org/wiki/Comet_%28programming%29
Ivan Kruchkoff