From what I'm reading, the defer
attribute on <script>
is now widely supported but I never see it used or mentioned.
If you don't need to defer inline scripts and don't add scripts dynamically (which cause problems in IE9- and Safari 4-), it seems that you could use it reliably and have scripts run right before DOMContentLoaded in the specified order (which doesn't happen with async
)
This is basically what most websites need: run a couple or more external scripts in sequence, on DOMready. For example:
<script defer src='jquery.js'></script>
<script defer src='jquery.some-plugin.js'></script>
<script defer src='my-scripts.js'></script>
Why isn't it widely used? Can I actually use it now?
</body>
tag and avoid all these issues ? – Acidulous</body>
" with "defer
scripts in<head>
". They even start downloading sooner, so there's an advantage in usingdefer
– Indecorumdefer
for deferred execution as compared to start downloading just before the body ends! – Clos