Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 8
    async is new(ish), but defer has been part of IE since IE4. defer was added to other browsers much more recently, but older versions of those browsers tend to hang around a lot less. Commented May 29, 2012 at 23:53
  • 5
    Now, HTML5 has become very popular! Commented Dec 28, 2016 at 8:41
  • 7
    defer is same as placing scripts at the bottom of the HTML, which has been common for many years. Commented Aug 23, 2018 at 18:30
  • 8
    @vsync not necessarily true, the browser will download the JS with the defer tag when it parses the script tag, but will defer exeuction until right before DOMContentLoaded. Downloading is non-blocking. Placing at the bottom of the HTML will delay downloading and execution of the JS until the DOM is constructed, but you will still incur an additional delay by waiting for the download. Commented Jun 25, 2019 at 14:34
  • 2
    @vsync I'm just arguing the defer is not the same as placing scripts at the bottom the HTML. When you set it to defer, the browser will download the JS in the background while it continues to construct the DOM. Once the DOM is constructed (DOMContendLoaded fired), the browser will then execute the JS that is has downloaded. That is what I meant by "non-blocking", e.g. it doesn't block the browser from constructing the DOM. This is why it is explicitly marked as defer. The image in an answer below explains it nicely. Commented Oct 7, 2019 at 20:59