Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1081600 of DOMContentLoaded

  • Revision slug: Web/Events/DOMContentLoaded
  • Revision title: DOMContentLoaded
  • Revision id: 1081600
  • Created:
  • Creator: psydexx1996
  • Is current revision? No
  • Comment

Revision Content

{{EventRef}}

The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event - load - should be used only to detect a fully-loaded page. It is an incredibly popular mistake to use load where DOMContentLoaded would be much more appropriate, so be cautious.

{{Note("Synchronous Javascript pauses parsing of the DOM.")}}

{{Note("There are also plenty of general-purpose and standalone libraries who offer cross-browser methods to detect that the DOM is ready.")}}

Speeding up

If you want DOM to get parsed as fast as possible after the user had requested the page, some things you could do is turn your JavaScript asynchronous and to optimize loading of stylesheets which if used as usual, slow down page load due to being loaded in parallel, "stealing" traffic from the main html document.

General info

Specification
HTML5
Interface
Event
Bubbles
Yes
Cancelable
Yes (although specified as a simple event that isn't cancelable)
Target
Document
Default Action
None.

Properties

{{OpenEventProperties}}{{CloseEventProperties}}

Example

<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
  });
</script>
<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
  });

for(var i=0; i<1000000000; i++)
{} // this synchronous script is going to delay parsing of the DOM. So the DOMContentLoaded event is going to launch later.
</script>

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0[1] {{CompatGeckoDesktop("1")}}[1] 9.0[2] 9.0 3.1[1]
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}}[1] {{CompatGeckoMobile("1")}}[1] {{CompatUnknown}}[2] {{CompatVersionUnknown}} {{CompatVersionUnknown}}[1]

[1] Bubbling for this event is supported by at least Gecko 1.9.2, Chrome 6, and Safari 4.

[2] Internet Explorer 8 supports the readystatechange event, which can be used to detect when the DOM is ready. In earlier versions of Internet Explorer, this state can be detected by repeatedly trying to execute document.documentElement.doScroll("left");, as this snippet will throw an error until the DOM is ready.

{{LoadRelatedEvents}}

Revision Source

<p>{{EventRef}}</p>

<p>The <code>DOMContentLoaded</code> event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.&nbsp;A very different event -&nbsp;<a href="/en-US/docs/Mozilla_event_reference/load"><code>load</code></a>&nbsp;-&nbsp;should be used only to detect a fully-loaded page. It is an incredibly popular mistake to use <a href="/en-US/docs/Mozilla_event_reference/load"><code>load</code></a> where <code>DOMContentLoaded</code> would be much more appropriate, so be cautious.</p>

<p>{{Note("Synchronous Javascript pauses parsing of the DOM.")}}</p>

<p>{{Note("There are also plenty of general-purpose and standalone libraries who offer cross-browser methods to detect that the DOM is ready.")}}</p>

<h2 id="Speeding_up">Speeding up</h2>

<p>If you want DOM to get parsed as fast as possible after the user had requested the page, some things you could do is turn your <a href="mailto:[email protected]">JavaScript asynchronous</a> and to <a href="#">optimize loading of stylesheets</a> which if used as usual, slow down page load due to being loaded in parallel, "stealing" traffic from the main html document.</p>

<h2 id="General_info">General info</h2>

<dl>
 <dt style="width: 120px; text-align: right; float: left;">Specification</dt>
 <dd style="margin: 0px 0px 0px 120px;"><a class="external" href="https://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#the-end">HTML5</a></dd>
 <dt style="width: 120px; text-align: right; float: left;">Interface</dt>
 <dd style="margin: 0px 0px 0px 120px;">Event</dd>
 <dt style="width: 120px; text-align: right; float: left;">Bubbles</dt>
 <dd style="margin: 0px 0px 0px 120px;">Yes</dd>
 <dt style="width: 120px; text-align: right; float: left;">Cancelable</dt>
 <dd style="margin: 0px 0px 0px 120px;">Yes (although specified as a simple event that isn't cancelable)</dd>
 <dt style="width: 120px; text-align: right; float: left;">Target</dt>
 <dd style="margin: 0px 0px 0px 120px;">Document</dd>
 <dt style="width: 120px; text-align: right; float: left;">Default Action</dt>
 <dd style="margin: 0px 0px 0px 120px;">None.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<p>{{OpenEventProperties}}{{CloseEventProperties}}</p>

<h2 id="Example">Example</h2>

<pre class="brush: html">
&lt;script&gt;
  document.addEventListener("DOMContentLoaded", function(event) {
&nbsp;   console.log("DOM fully loaded and parsed");
  });
&lt;/script&gt;
</pre>

<pre class="brush: html">
&lt;script&gt;
  document.addEventListener("DOMContentLoaded", function(event) {
&nbsp;   console.log("DOM fully loaded and parsed");
  });

for(var i=0; i&lt;1000000000; i++)
{} // this synchronous script is going to delay parsing of the DOM. So the DOMContentLoaded event is going to launch later.
&lt;/script&gt;
</pre>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0<sup>[1]</sup></td>
   <td>{{CompatGeckoDesktop("1")}}<sup>[1]</sup></td>
   <td>9.0<sup>[2]</sup></td>
   <td>9.0</td>
   <td>3.1<sup>[1]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
   <td>{{CompatGeckoMobile("1")}}<sup>[1]</sup></td>
   <td>{{CompatUnknown}}<sup>[2]</sup></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Bubbling for this event is supported by at least Gecko 1.9.2, Chrome 6, and Safari 4.</p>

<p>[2] Internet Explorer 8 supports the <code>readystatechange</code> event, which can be used to detect when the DOM is ready. In earlier versions of Internet Explorer, this state can be detected by repeatedly trying to execute <code>document.documentElement.doScroll("left");</code>, as this snippet will throw an error until the DOM is ready.</p>

<h2 id="Related_Events">Related Events</h2>

<p>{{LoadRelatedEvents}}</p>
Revert to this revision