Deferred JavaScript execution

Any script element marked with defer will not execute until after the DOM has been completely loaded; this holds true for inline scripts as well as for external script files.

<html>
<head>
<title>Script Defer Example</title>
</head>
<body>
<script defer>
alert("defer");
</script>
<script>
alert("script");
</script>
<script>
window.onload = function(){
alert("load");
};
</script>
</body>
</html>

In browsers that don’t support defer, the order of the alerts is “defer”, “script”, and “load”. In browsers that support defer, the order of the alerts is “script”, “defer”, and “load”.

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.