Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>jQuery's <code>ready</code> event for the document fires as soon as jQuery determines that the DOM is accessible. The exact mechanism depends on the browser.</p> <p>Take a look at the relevant <a href="http://james.padolsey.com/jquery/#v=1.7.2&amp;fn=jQuery.bindReady" rel="nofollow">source code</a>.</p> <p>Firstly, there is a check to see if the DOM is already accessible at the point where an attempt was made to bind a listener to the event. If it is, the callbacks are scheduled to fire immediately - although they are not actually <em>fired</em> immediately, to allow the code already occupying the current execution slot to cancel the handlers if required.</p> <p>If the DOM is not yet accessible, an attempt is made to bind an event listener to the browser's native <a href="https://developer.mozilla.org/en-US/docs/Web/Reference/Events/DOMContentLoaded" rel="nofollow"><code>DOMContentLoaded</code></a> event - this is the "correct" native way to ask the browser to inform you when the DOM is available, but it's a relatively modern feature. If this is not possible (this almost certainly indicates you code is running in an older version of IE), the code falls back to a couple of mechanisms:</p> <ul> <li>Try and attach to the <code>onreadystatechange</code> event of the document. This is not fool-proof and will be later than <code>DOMContentLoaded</code> would have been, but it's pretty good.</li> <li>Fall back to the <code>load</code> event of the <code>window</code> object. This will often be much later than the DOM is available, but it's a last-ditch failsafe to ensure the event will always fire eventually.</li> <li>The worst case scenario: keep polling the DOM until it's accessible.</li> </ul> <p>From a PHP perspective, it is <em>possible</em> (but unlikely) for this to occur before your PHP script has finished executing. There are situations (such as long-polling) where the event would fire before your script is finished, but this would only occur in older browsers. However, in these scenarios, you wouldn't (shouldn't) be using these events at all, you would simply place the appropriate <code>&lt;script&gt;</code> elements in the body of the page to allow them to be executed as soon as they are loaded, without waiting for the rest of the DOM.</p> <p>Personally, I never use any of these load-driven events, more or less for that reason. YMMV.</p>
 

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