Skip to content

Instantly share code, notes, and snippets.

@gx0r
Last active February 28, 2017 16:49
Show Gist options
  • Save gx0r/927912987aaf708234f46aedae8c5a91 to your computer and use it in GitHub Desktop.
Save gx0r/927912987aaf708234f46aedae8c5a91 to your computer and use it in GitHub Desktop.
DOMContentLoaded vs jQuery
<html>
<head>
<title>DOMContentLoaded vs jQuery</title>
</head>
<body>
<h1>
With jQuery (2.1.4) if a function throws in a $(document).ready listener, it will prevent subsequent
$(document).ready listeners from executing. This demonstrates that it does not happen with jQuery 3.1.1 or document.addEventListener.
</h1>
<script>
document.addEventListener('DOMContentLoaded', function () {
console.log('here');
throw new Error('DOM - intentional');
});
document.addEventListener('DOMContentLoaded', function () {
console.log('here2');
var div = document.createElement('div');
div.innerText = 'The throws clause in another DOMContentLoaded did not prevent this one from running.';
document.body.appendChild(div);
});
</script>
<script src="https://code.jquery.com/jquery-2.1.4.js" integrity="sha384-1qy6pxCPVEhkjPJM8mBaaRNIDGE20UzrPyndMEoCaeK390vhZ3jt3SQtS6aZDqRA" crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
console.log('jquery here1');
throw new Error('jQuery - intentional');
});
$(document).ready(function () {
console.log('jquery here2');
var div = document.createElement('div');
div.innerText = 'The throws clause in another $(document).ready handler did not prevent jQuery 2.1.4 from running this one.';
document.body.appendChild(div);
});
</script>
<script
src="https://code.jquery.com/jquery-3.1.1.js"
integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
throw new Error('jQuery - intentional');
});
$(document).ready(function () {
console.log('jquery here2');
var div = document.createElement('div');
div.innerText = 'The throws clause in another $(document).ready handler did not prevent jQuery 3.1.1 from running this one.';
document.body.appendChild(div);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment