Code Snippet

Home » Code Snippets » jQuery » Run JavaScript Only After Entire Page Has Loaded

Run JavaScript Only After Entire Page Has Loaded

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

Subscribe to The Thread

  1. 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.

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

  2. Rodrigo

    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

      :- )

  3. ChromA

    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.

  4. mathew

    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

  5. // just another way to write the same thing.

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

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

  7. 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*/

  8. Genius! Thank you so much :-)

  9. 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

  10. 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

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~