tags:

views:

94

answers:

6

the program needs invoke a function after all code, including HTML, javascript, CSS, etc., is loaded? Can javascript do it?

A: 

If you're using the jQuery library, you simply do this:

$(document).ready(function() {
  // The code you need to have executed after loading the page
});
Gert G
The [`$(document).ready`](http://api.jquery.com/ready/) event fires just after the parsing of the document is finished, that's usually *before* other resources like images and subframes are loaded.
CMS
-1 Why push JQuery on people who didn't mention it? Perhaps JQuery is out of scope for either the project or the person asking the question's level of comfort?
Chris Sobolewski
@CMS, true but the DOM is ready for traversal at that point and new nodes can be added without any adverse effect, in most cases.
Chris Sobolewski
@Chris, yes I know, but the OP *specifically wants* to know when *all* the resources are loaded.
CMS
@CMS - Point taken.
Gert G
A: 
window.onload = function() {
  // Your code here
};

What have you tried?

artlung
A: 

You can use <body onload="doStuff()">, or you can use window.onload in your script. Check this out.

Babiker
A: 

The jQuery $(document).ready(...) method is triggered when the dom is loaded and can be manipulated and before all scripts, images, etc. are loaded.

The window.onload event will fire when everything that has been requested has completed loading.

jhorback
+3  A: 

for JavaScript

window.onload = function(){
   //your code
};

for JQuery

$(document).ready(functino(){
  //your code
});
Spring
`window.onload` and `$(document).ready` are not equivalent, the first one does what the OP wants, but the second, will fire early, just after the DOM parsing is done (images and other resources might not be loaded yet), the jQuery equivalent would be `$(window).load(function () {/*...*/});`
CMS
@CMS Which would take a performance hit due to waiting for images to be loaded.
Gert G
The jQuery ready function is nice. I've tried messing with all sorts of alternatives and its the best. Plus you get to use jQuery - (ok I'm biased, but its a great library!)
dana
@Greg, of course, but that's the *specific requirement* of the OP, I guess he is doing something that relies on the images being already loaded... only he knows...
CMS
I guess Greg is me... :D ... He never stated that images needed to be loaded, just that all the code should be loaded.
Gert G
+3  A: 

window.onload will fire after all images, frames and objects have finished loading on the page. Your question isn't clear enough on whether or not you want the script to wait for those, but if you don't then you need a "document ready" solution.

Firstly, many (all?) DOM-based Javascript frameworks provide this functionality, cross browser in the form of an event. jQuery example:

$(document).ready(function() {
    alert("DOM is ready");
});

If you want to do it without the framework, it gets a little more awkward. Most browsers (*cough*notIE) provide a DOMContentLoaded event:

if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', function () {
        alert("DOM is ready");
    }, false);
}

For IE's part, the defer attribute on a script tag will do the job. You can use conditional comments to make sure only IE parses the script:

<!--[if IE]
<script type="text/javascript" defer>
    alert("DOM is ready");
</script>
<![endif]-->
Andy E