treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Run JavaScript Only After Entire Page Has Loaded

Last updated on:

$(window).bind("load", function() {
   // code here
});
View Comments

Comments

  1. This should go in jQuery

  2. A shorthand version of this is:

    $(function() {
    // code here
    }

    • There is a fundamental difference there. That is is when the DOM is ready, which is usually ideal. This is when the whole page has finished loading, which is different. For example, this will wait for images to be finished loading, so that you can measure their widths and heights accurately while your snippet will not wait. Small but sometimes crucial difference.

    • Permalink to comment#

      $(function() {
      // code here
      }

    • Makaze
      Permalink to comment#

      Your syntax is incorrect as well. It should be:

      $(function() {
      // code here
      });

      Be sure to close those brackets and end with a semicolon.

  3. Rodrigo
    Permalink to comment#

    This is probably my first comment on your site and I’ve been a fan and reading for a long while probably since podcast 20 XD

    I haven’t been able to figure out where to place this code..?

    If you can help me out, give me an example with your “simple jquery accordion” – http://css-tricks.com/snippets/jquery/simple-jquery-accordion/

    Thanks, Chris!

    • Place the code in your <head>HERE</head> section.

    • it depends how much code you have and if you are erver going to reuse. If you want to reuse put function in the JS file with reference to a variable that lives in

      Then you can put any id you want per page and if item of this id exists it will be processed.

      so in JS file you dont hardcode stuff that much just check if varialbe is ther and what is the value.

      for this example i guess it would be an overkill

      :- )

  4. ChromA
    Permalink to comment#

    Is there way to do this without jQuery? I use jQuery (but the script will be loaded at page-end). I’m looking for a JS-Snippet to use in the Content (that will be executed when the page has loaded.

  5. mathew
    Permalink to comment#

    Small thing. When using the button “copy to clipboard” it will copy it with curly ‘”‘ and not just straight “. With the curly ones the code doesn’t work…

    Thanks

  6. jon
    Permalink to comment#

    // just another way to write the same thing.

    $(window).load(function() {
    // everything’s finished loaded code here…
    });

  7. how can i add this to blogger template??

  8. bekir
    Permalink to comment#

    I’m trying to load page before load the map ( I use is map extreme ) although I use document ready function, the page trying to load mapLabels with page objects. for a while user see my page as there is no css. After the map finished it’s job the css loaded. How can I fix this? I use your code but it didn’t work for me.

    Here is an example;

    j(window).bind(“load”, function () {
    MapXtremeJavascriptLib.init();
    });

    /* DOCUMENT READY FUNCTION AREA START */
    j(document).ready(function () {

    j(“.accordion”).accordion({
    autoHeight: true,
    navigation: true,
    collapsible: true
    });
    /* DOCUMENT READY FUNCTION AREA END*/

  9. Genius! Thank you so much :-)

  10. Permalink to comment#

    Hi all,
    I’m battering my head about how I can A: Tell a javascript to wait till the entire site has loaded. I guess I just found the solution to that. And B: Have the javascript load only on the home page.

    The site runs on joomla and before I had the script run from a custom html module which I had assigned to the debug position and set to only load on the home page.

    That worked fine for firefox, chrome and safari but IE was mocking up (as usual).

    Now I’m battering my head about how I can add it so that the script (a lady walking into the site and welcoming the visitors) will only show on the home page.

    I appreciate any suggestions!

    Thanks in advance,
    Andy

  11. Permalink to comment#

    thanks, chris before knowing this I used setTimeout, and the bad thing using this, is we never exactly know how long the page load time on the user browser

  12. Permalink to comment#

    Shame it doesn’t work in IE9 :-(

    • Ben Riordan

      $(window).load(function()

      Should work fine on IE9. At least it is for me. Doesn’t work on IE8 and below though.

      What it doesn’t work on however if the current version of Firefox. Apparently firefox treats .load and .ready as the same and loads .load after the javascript has loaded and doesn’t wait for all images and other page content like the other browsers do. Does anyone know of a workaround for firefox?

      Thanks

      ~ Ben

  13. Dan Voyce
    Permalink to comment#

    Your averaging 1 answer a day for me. Thank you very much!

  14. RITURAJ
    Permalink to comment#

    working…..

  15. This is exactly what I needed, Thanks for the great resource!

  16. Holy crap. This just saved my life.

    … yes, I might be exaggerating, but not by much.

  17. After spending about four hours trying to get some externally sourced code to cycle with jQuery cycle, this snippet solved everything.

    Thanks!

  18. Great… jQuery again. Looking at the title in Google, I thought you were going to present something non-framework. Bah, why do people ignore the fact that it’s not cool to load a 32KB jQuery lib for something that could be done in a few lines.

    Thanks anyway… even when this didn’t help me at all.

    • You and your users probably have jQuery cached on their systems so I guess not an issue – plus if you use Google as the CDN its blindingly fast – so why not?

  19. Romans
    Permalink to comment#

    This is the most awesome website about web development!

  20. I want to Load my Minified JS after the page load.
    Currently it takes 1.8 sec to 2.5 sec to load this JS File which is increasing my complete webpage load to 4.2 sec to 5.1 sec.

    So how can i load this JS without delaying the page load.

  21. Doug T
    Permalink to comment#

    As always, jump on google have a nosey and the first result from Css-Tricks is the winner on the day!, Cheers for your time invested in the web dev commnity Chris.

  22. Jubal
    Permalink to comment#

    $(window).on(‘load’, function(){
    // AWESOMENESS
    })

  23. This snippet made my day Chris thank you a many man, I’m finding more and more useful things on your website for my front-end development, thanks !!

  24. Thank you! Been struggling to get Jquery to load after a ton of images have been loaded and this just works perfectly!

  25. David
    Permalink to comment#

    As Jubal points out .bind has been deprecated in flavor of .on

  26. Riya

    $(window).bind(“load”, function(){
    });
    The above function works fine only on the initial load. But when we navigate to differect page and come back to the same page. the function gets executed before complete loading of Dom elements. Kindly tell me if there is another way

Leave a Comment

Use markdown or basic HTML and be nice.