views:

40

answers:

3

When a page is finished loading. Then I want to load a module other, smaller, inside it using jQuery. What should I do?

+2  A: 

It sounds like you want to use $.getScript(url)

The Who
+2  A: 

If you want to wait for the every asset to finish loading, then do something like this:

$(window).load(function(){
    $.getScript('/path/to/file.js');
});

You can also use an optional callback method:

$(window).load(function(){
    $.getScript('/path/to/file.js', function(){
        //Called when script is loaded
    });
});

jQuery docs for $.getScript()

Just to be clear, $(window).load() fires when everything on the page has finished loading including other scripts and images. $(document).ready() fires as soon as the DOM can be manipulated, which is often much sooner than $(window).load().

Doug Neiner
A: 
$(function() { $.getScript(url) });
Scott Evernden
This is the `$(document).ready()` event function and is called when the DOM is ready to be manipulated, not when the page has finished loading per se.
Doug Neiner