您可以在 HTML 脚本标签上同时使用 async 和 defer 属性吗? [英] Can you use both the async and defer attributes on a HTML script tag?

查看:21
本文介绍了您可以在 HTML 脚本标签上同时使用 async 和 defer 属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时使用 deferasync 加载以下 JavaScript 代码:

I would like to load in the following JavaScript code using both defer and async:

由于 defer 受 Internet Explorer 5.5+ 支持,正如您在 CanIUse 中所见.com,如果异步不可用,我想优雅地回退到使用延迟.异步我认为最好在它可用时使用,但直到 Internet Explorer 10 才支持它.

Since defer is supported by Internet Explorer 5.5+, as you can see at CanIUse.com, I would like to gracefully fallback to using defer if async is not available. Async I think is better to use when it is available but its not supported until Internet Explorer 10.

因此,我的问题是上述代码是否有效 HTML?如果没有,是否有可能使用 JavaScript 在 async 不可用时优雅地回退到在脚本上使用 defer 来创建这种情况?

My question is therefore is the above code valid HTML? If not, is it possible to create this situation using JavaScript of gracefully falling back to using defer on a script when async is not available?

推荐答案

来自规范:https://www.w3.org/TR/2011/WD-html5-20110525/scripting-1.html#attr-script-async

即使指定了 async 属性,也可以指定 defer 属性,以导致仅支持 defer(而非异步)的旧版 Web 浏览器回退到 defer 行为,而不是默认的同步阻塞行为.

The defer attribute may be specified even if the async attribute is specified, to cause legacy Web browsers that only support defer (and not async) to fall back to the defer behavior instead of the synchronous blocking behavior that is the default.

(查看下面的参考链接,直观地了解普通脚本与使用 defer 和 async 的脚本之间的差异)

参考文献:

这篇关于您可以在 HTML 脚本标签上同时使用 async 和 defer 属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆