最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Any good reason not to use <script defer> TODAY? - Stack Overflow

programmeradmin0浏览0评论

Once upon a time, there were many heated debate on <script> in <head> or <body>.

Many SO posts had already pointed out the best practice / rule of thumb is to place the <script> before end of <body>for not blocking the html parser, resulting faster first screen paint and quicker DOM access for client, and thus a better UX.

This must be a duplicate╰(‵□′)╯

Wait ... <script> can be deferred now, actually for quite a while !

Old posts said

deferred script may results JS dependency issues

No, it won't. It retains the order on execution immediately when the DOM gets parsed.

It doesn't work cross vendors

Yes, it once was, but today it's almost supported by all major browser vendors: , besides may have some problem with IE<10 as the comments point out.

However, the benefits it offers seem obvious, at least to me, as it downloads the script in parallel at earlier time(before start parsing the DOM), thus no need to request the script later and shortens the time it takes to bring the whole page interactive.

To be short, this question is similar to: Any good reason not to use

<head>
...
<script src='cdn/to/jquery' defer>
<script src='cdn/to/bootstrap' defer>
<script src='script/depends/on/jqueryandbootstrap' defer>
</head>

instead using this:

<body>
...
<script src='cdn/to/jquery'>
<script src='cdn/to/bootstrap'>
<script src='script/depends/on/jqueryandbootstrap'>
</body>

note: This might be an "ancient" topic with lots of discussions. However, as web technology moves really fast, browser vendors align better and more consistent with new web specs, many old stackoverflow answers may not keep up-to-date.

Once upon a time, there were many heated debate on <script> in <head> or <body>.

Many SO posts had already pointed out the best practice / rule of thumb is to place the <script> before end of <body>for not blocking the html parser, resulting faster first screen paint and quicker DOM access for client, and thus a better UX.

This must be a duplicate╰(‵□′)╯

Wait ... <script> can be deferred now, actually for quite a while !

Old posts said

deferred script may results JS dependency issues

No, it won't. It retains the order on execution immediately when the DOM gets parsed.

It doesn't work cross vendors

Yes, it once was, but today it's almost supported by all major browser vendors: http://caniuse.com/#search=defer, besides may have some problem with IE<10 as the comments point out.

However, the benefits it offers seem obvious, at least to me, as it downloads the script in parallel at earlier time(before start parsing the DOM), thus no need to request the script later and shortens the time it takes to bring the whole page interactive.

To be short, this question is similar to: Any good reason not to use

<head>
...
<script src='cdn/to/jquery' defer>
<script src='cdn/to/bootstrap' defer>
<script src='script/depends/on/jqueryandbootstrap' defer>
</head>

instead using this:

<body>
...
<script src='cdn/to/jquery'>
<script src='cdn/to/bootstrap'>
<script src='script/depends/on/jqueryandbootstrap'>
</body>

note: This might be an "ancient" topic with lots of discussions. However, as web technology moves really fast, browser vendors align better and more consistent with new web specs, many old stackoverflow answers may not keep up-to-date.

Share Improve this question edited Jun 3, 2017 at 16:23 Allen asked Mar 1, 2017 at 22:57 AllenAllen 4,7793 gold badges29 silver badges42 bronze badges 11
  • 6 there's a note in caniuse that IE < 10 could be broken - in my opinion, that's a good reason to start using defer now :p – Jaromanda X Commented Mar 1, 2017 at 23:02
  • 5 Here is some good reading. – Pointy Commented Mar 1, 2017 at 23:03
  • 3 it will be the same as putting it in the head of head without defer – Ibu Commented Mar 2, 2017 at 0:08
  • 2 I nearly forgot this(defer) magic attribute; This is an interesting article I found on Mozilla Hack – Bhavik Commented Mar 2, 2017 at 0:39
  • 1 would defer remove the need for $(document).ready()? – Shane A. Workman Commented Sep 11, 2017 at 13:29
 |  Show 6 more comments

2 Answers 2

Reset to default 10

Yes, but only because you're using jQuery.

jQuery doesn't work with defer because it tries to fire as soon as the page becomes interactive. They can't fix it any time soon (I raised that bug over a year ago) because changing the ready behaviour to work with defer will break lots of components that rely on jQuery's ready event firing on interactive (i.e. before deferred scripts have finished loading).

If you're using a more modern framework (React, Angular 2, Polymer, Vue, or just about anything else) then go for it - or even go to the next step and use <script type=module in new browsers and a legacy bundle in <script nomodule defer... for IE.

As noted here you should take browser support into account, as some of them don't really support it. There are also some well known bugs in some versions like this one in IE<=9.

If your goal is not to support old browsers (find full support list here) then there is no real reason for not picking defer today.

发布评论

评论列表(0)

  1. 暂无评论