Using SCRIPT’s defer Attribute

By  on  

One of the seldom used attributes within the HTML tag library is the defer attribute on SCRIPT elements.  As you can probably tell by the name of the attribute, defer instructs the contents of the script tag to not execute until the page has loaded.  Take a look!

Deferring Your Scripts

<script>
	//do stuff (runs first)
</script>
<script defer="defer">
	//do stuff, but defer it  (runs last)
</script>
<script>
	//do more stuff (runs second)
</script>

The deferred SCRIPT element's code will execute once the rest of the page's resources have loaded.  What does this mean?  Be sure that your document doesn't rely on any of the code within the script during page load.  In the example above, the middle block will execute once the page has loaded even though it appears before the last block.

More Details

Olivier Rochard has written an outstanding (and more detailed) post about using the defer attribute on the Mozilla Hacks blog.  His post details browser support (and quality of browser support...or lack thereof), advanced examples, and tips for using the defer attribute.

What I find funny about this tag is that it seems as though most of the script I see should be using this attribute.

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    39 Shirts &#8211; Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

Incredible Demos

  • By
    MooTools-Like Element Creation in jQuery

    I really dislike jQuery's element creation syntax. It's basically the same as typing out HTML but within a JavaScript string...ugly! Luckily Basil Goldman has created a jQuery plugin that allows you to create elements using MooTools-like syntax. Standard jQuery Element Creation Looks exactly like writing out...

  • By
    CSS Vertical Centering

    Front-end developing is beautiful, and it's getting prettier by the day. Nowadays we got so many concepts, methodologies, good practices and whatnot to make our work stand out from the rest. Javascript (along with its countless third party libraries) and CSS have grown so big, helping...

Discussion

  1. It’s seldom used because according to the blog post you linked to the only browser that executed the defer tag correctly is FireFox. IE and Safari have erratic behavior.

    Then one of the last comments on that post points to a bug report that says that HTML5 requires defer to be ignored https://bugzilla.mozilla.org/show_bug.cgi?id=518104

    This seems like a useless tag now.

  2. There is another good article on this over at http://dean.edwards.name/weblog/2005/09/busted/

    Beware of defer, it can be useful if used right, but it is easily used incorrectly

  3. I don’t think “defer” is reliable enough to be used effectively and to think that this means you’ll have better page load performance across the board. Also, the quirks in it can lead to some crazy-to-track-down race conditions.

    Dynamic script loaders are, IMHO, superior to “defer” (or even HTML5’s “async”), because if done right they provide a reliable and normalized behavior across all browsers.

    Some good examples would be LABjs, RequireJS and Dominoes.

    Also, many of the frameworks have script loaders that work well.

  4. kolin

    how would this fit in with ‘domready’ and ‘load’ ?
    i usually run scripts ‘domready’ or ‘load’ depending on whether the browser is trident. could defer replace both?

  5. Wow, I didn’t know you could actually use this outside of IE. Though the behavior seems so unpredictable with certain browsers, it kinda scares me to use it.

  6. Pranav Dave

    Using “defer” has resolved my issue of “Operation Abort” in IE6 and IE7.. Its good to use it always.. as it will not allow anything to write in DOM untill the page is completely loaded….

    • serhio

      not always. if your script does a document.write is harmfull

  7. Javascript Training

    The defer attribute is not well supported in all major browsers. So it should not be used.

  8. Javascript Training

    The defer attribute is not well supported in all major browsers. So it should not be used.

    http://wisentechnologies.com/it-courses/html-css-javascript-jquery-training.aspx

  9. I’m afraid the “defer” attribute is useless.

  10. serhio

    Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).

    So I don’t see the meaning of using it as in the example….

    http://www.w3schools.com/tags/att_script_defer.asp

  11. Wow, I didn’t know you could actually use this outside of IE

  12. James

    This doesn’t work, never has, and never will. The scripts have to be externally loaded for it to behave in the way described as noted by serhio. Please remove this example; it is highly ranked in Google and is wrong.

  13. defer is rarely used anymore, they now use async, both didn’t help my pagespeed score

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!