Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the way jQuery wraps the functions you're looking for - the snippet does not need jQuery, and is cross-browser compatible. I've replaced all calls to jQuery.ready() with <code>yourcallback</code> - which you need to define.</p> <p>What goes on in here:</p> <ul> <li>first, the function <code>DOMContentLoaded</code> is defined, which will be used when the DOMContentLoaded event fires - it ensures that the callback is only called once.</li> <li>a check if the document is already loaded - if yes, fire the callback right away</li> <li>otherwise sniff for features (<code>document.addEventListener</code> / <code>document.attachEvent</code>) and bind the callbacks to it (different for IE and normal browsers, plus the onload callback)</li> </ul> <p><a href="http://code.jquery.com/jquery-1.4.3.js">Lifted from jQuery 1.4.3, functions bindReady() and DOMContentLoaded</a>:</p> <pre><code>/* * Copyright 2010, John Resig * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license */ // Cleanup functions for the document ready method // attached in the bindReady handler if ( document.addEventListener ) { DOMContentLoaded = function() { document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); //jQuery.ready(); yourcallback(); }; } else if ( document.attachEvent ) { DOMContentLoaded = function() { // Make sure body exists, at least, in case IE gets a little overzealous if ( document.readyState === "complete" ) { document.detachEvent( "onreadystatechange", DOMContentLoaded ); //jQuery.ready(); yourcallback(); } }; } // Catch cases where $(document).ready() is called after the // browser event has already occurred. if ( document.readyState === "complete" ) { // Handle it asynchronously to allow scripts the opportunity to delay ready //return setTimeout( jQuery.ready, 1 ); // ^^ you may want to call *your* function here, similarly for the other calls to jQuery.ready setTimeout( yourcallback, 1 ); } // Mozilla, Opera and webkit nightlies currently support this event if ( document.addEventListener ) { // Use the handy event callback document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); // A fallback to window.onload, that will always work //window.addEventListener( "load", jQuery.ready, false ); window.addEventListener( "load", yourcallback, false ); // If IE event model is used } else if ( document.attachEvent ) { // ensure firing before onload, // maybe late but safe also for iframes document.attachEvent("onreadystatechange", DOMContentLoaded); // A fallback to window.onload, that will always work window.attachEvent( "onload", yourcallback ); } </code></pre> <p>That's 51 lines of pure JavaScript code, just to register the event reliably. As far as I know, there is no easier method. Goes to show what the wrappers like jQuery are good for: they wrap the capability sniffing and ugly compatibility issues so that you can focus on something else.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload