Cross Platform DOM Ready

Aug 8, 2017
Equivalent of document.onload or DOMContentLoaded.

TL;DR

Use contentloaded.js by dperiri.

<script>contentLoaded(window, function() {  console.log("loaded");});</script>

If you can use jQuery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><script>$(document).ready( function() {   console.log("loaded");}); </script>

If you don't want any dependency, use window.onload (not really DOM ready, but the entire page is loaded including css, scripts, images, etc.).

<script>window.onload = function() {  console.log("loaded");};</script>

window.onload

window.onload is the most widely supported, where it's called when the page loads all its content (images, css, scripts, etc.).

<script>window.onload = function() {  console.log("loaded");};</script>

document.onload

document.onload is triggered when the DOM is ready, but it might not be well supported on all browser.

<script>document.onload = function() {  console.log("loaded");};</script>

DOMContentLoaded

DOMContentLoaded is triggered when the DOM is ready, but support is not universal (minimum of IE9 is required).

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

Using jQuery

jQuery provide well good cross browser support.

Equivalent of document.onload (DOM ready).

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><script>$(document).ready( function() {   console.log("loaded");}); </script>

Equivalent of window.onload (Page ready).

<script>$(window).load( function() {   console.log("loaded");});</script>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.