Skip to content

Instantly share code, notes, and snippets.

@passcod
Created May 26, 2012 08:29
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save passcod/2792934 to your computer and use it in GitHub Desktop.
Save passcod/2792934 to your computer and use it in GitHub Desktop.
onload, readyState, and DOMContentLoaded
<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function () {
console.log(''+(+new Date)+': Onload fired');
};
document.onreadystatechange = function () {
console.log(''+(+new Date)+': Ready state changed');
if (document.readyState == "complete") {
console.log(''+(+new Date)+': Ready = Complete');
}
};
window.addEventListener("DOMContentLoaded", function () {
console.log(''+(+new Date)+': DOM Content Loaded');
});
</script>
</head>
<body>
<h1>Test</h1>
<p>To see the difference between <code>onload</code> and <code>readyState == "complete"</code>
and <code>DOMContentLoaded</code>.</p>
<p>Run me with Firebug or Chrome Dev Tools, and cache disabled, and watch the console.</p>
<hr />
<img src="http://i.imgur.com/owuVS.gif" />
<img src="http://i.imgur.com/1sE1n.jpg" />
<img src="http://i.imgur.com/A2s1e.jpg" />
<img src="http://i.imgur.com/BaFin.jpg" />
</body>
</html>
@jasonwilliams
Copy link

Performance.now() would be more useful than new Date, and its more accurate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment