Super-handy jQuery code snippet to run JavaScript after a complete page has loaded

Nothing to do with WordPress directly but if you’re looking to run some JavaScript in a template/file somewhere AFTER everything on a page has completely loaded then this could be just the thing:

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

That’s it – end of post!

By WinningWP Editorial

Run by Brin Wilson, WinningWP is an award-winning resource for people who use – you guessed it – WordPress. Follow along on Twitter and/or Facebook.
Comments (policy)
  1. $(document).ready(function(){ //code });

    is another handy way to run code when the page is fully loaded. If you need to make sure you’re running jQuery no-conflict, you can use

    jQuery(document).ready(function($){ //code });

  2. Juan Olvera says:

    From the jQuery API documentation “As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.”

    Right now we are at “1.11.1” with legacy support and “2.1.1” for modern browsers only.

    This means that:

    “`
    // prevent the $ from conflicting with other JavaScript libraries.
    jQuery.noConflict();
    (function($) {

    // Works same as $(document).ready(function() { });
    $(function() {
    // Your code here
    });
    })(jQuery);
    “`

    Would be a better option, the `.bind();` method is used only for legacy purposes.

Leave a Reply

Your email address will not be published. Required fields are marked *

All comments are held for moderation. We'll only publish comments that are on topic and adhere to our Commenting Policy.

Our commenting form also supports the use of Markdown.