views:

6913

answers:

11

I'm executing an external script, using a <script> inside <head>.

Now since the script executes before the page has loaded, I can't access the <body>, among other things. I'd like to execute some JavaScript after the document has been "loaded" (HTML fully downloaded and in-RAM). Are there any events that I can hook onto when my script executes, that will get triggered on page load?

+9  A: 

Look at hooking document.onload or in jQuery $(document).load(...).

Daniel A. White
Is this event reliable? (Cross browser.. IE6+ FF2+ etc)
Jenko
Yes this is cross-browser, standard DOM.
Daniel A. White
It's actually window.onload that's more standard, not document.onload. AFAIK
Peter Bailey
Thanks for your correction.
Daniel A. White
+1  A: 

Just define <body onload="aFunction()"> that will be called after the page has been loaded. Your code in the script is than enclosed by aFunction() { }

Norbert Hartl
I was too hasty and forgot to use < instead of <. Sorry, but it all went soo fast
Norbert Hartl
Well is it different in comments? <bla>
Norbert Hartl
+7  A: 
<body onload="script();">

or

document.onload=function ...

edit: according to Peter Bailey in a comment here

window.onload = function ...

is a better way to go

marcgg
A: 

jQuery wrappers that for you. You'll probably find it to be the easiest solution.

Josh
+3  A: 

You can put a "onload" attribute inside the body

...<body onload="myFunction()">...

Or if you are using jQuery, you can do

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

or 

$(window).load(function(){ /*code here*/ })

I hope it answer your question.

Note that the $(window).load will execute after the document is rendered on your page.

Nordes
A: 

Check my answer here.

There's a bunch of other info in that question about executing javascript after page load.

Peter Bailey
A: 

As Daniel says, you could use document.onload.

The various javascript frameworks hwoever (jQuery, Mootools, etc.) use a custom event 'domready', which I guess must be more effective. If you're developing with javascript, I'd highly recommend exploiting a framework, they massively increase your productivity.

Iain
Pity browsers didn't do what frameworks do.
Jenko
+4  A: 

Reasonably portable, non-framework way of having your script set a function to run at load time:

if(window.attachEvent) {
    window.attachEvent('onload', yourFunctionName);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            yourFunctionName();
        };
        window.onload = newonload;
    } else {
        window.onload = yourFunctionName;
    }
}
chaos
+1 for posting almost exactly what I had to come up with 6 months ago. Code like this can be necessary if other frameworks and code that you have no control over are adding onload events and you want to as well without wiping out the other onload events. I included my implementation as a function and it required var newonload = function(evt) { curronload(evt); newOnload(evt); } because for some reason the framework I am using requires an event to be passed to the onload event.
Grant Wagner
I just discovered in testing that the code as written results in handlers being fired in an undefined order when attached with attachEvent(). If order-of-handler-execution is important you may want to leave out the window.attachEvent branch.
Grant Wagner
@Grant Wagner: Interesting! Good find.
chaos
+1  A: 

Using the YUI library (I love it):

YAHOO.util.Event.onDOMReady(function(){
    //your code
});

Portable and beautiful! However, if you don't use YUI for other stuff (see its doc) I would say that it's not worth to use it.

N.B. : to use this code you need to import 2 scripts

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js" ></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/event/event-min.js" ></script>
Valentin Jacquemin
A: 

//It's tested and working :)

$(document).ready(function() { functon1(); function2() });

A: 

All of these helps do not answer the question: How to run JS script "AFTER" the page load. The onload is executed on the event the page is loaded. I want to run after that. For example, a page has a bunch of scripts run on load. Those can generate further DOM elements. I want to run after all of those functions. How do I do that?

Nan