`script`和`link as =" script"`标签之间的区别 [英] Difference between `script` and `link as="script"` tags
问题描述
除了加载脚本的标准方法之外:
Additionally to the standard method of loading scripts:
<script src="js/script.js"></script>
我见过人们这样做:
<link href="js/script.js" as="script">
有什么区别吗?
注意:有类似的 使用链接和脚本标记引用JavaScript源代码之间的区别? 询问< link href =〜/ Scripts / jquery-1.4.1.jstype =text / javascript/>
,这是不同的。
Note: There's a similar What's the difference between using link and script tag to reference JavaScript source? question asking about <link href="~/Scripts/jquery-1.4.1.js" type="text/javascript" />
, which is different.
推荐答案
如果那个链接
标签在它上面有 rel =preload
(或 rel =modulepreload
),它会指示预加载请求,这将预先加载,但不会运行脚本。当然,脚本
加载并运行脚本。但是如果没有 rel
,则链接
无效且无效(至少在规范方面)。
If that link
tag had rel="preload"
(or rel="modulepreload"
) on the it, it would indicate a preload request, which would preload, but not run, the script. Whereas of course, script
loads and runs the script. But without rel
, that link
is invalid and has no useful effect (at least in terms of the specification).
如果您查找规范中的链接
,你会看到为
列为:
as
- 预加载请求的潜在目标(对于rel =preload
和rel =modulepreload
)
as
— Potential destination for a preload request (forrel="preload"
andrel="modulepreload"
)
Following the link to as
attribute, it says:
as
属性指定潜在目的地由href
属性给出的资源的预加载请求。它是枚举属性。每个潜在目标都是此属性的关键字,映射到同名状态。必须在link
元素上指定该属性,该元素具有包含preload <的
rel
属性/ code>关键字。它可以在link
元素上指定,它们具有rel
属性,其中包含modulepreload
keyword;在这种情况下,它必须具有类似脚本的目标值。对于其他链接
元素,不得指定它。
The
as
attribute specifies the potential destination for a preload request for the resource given by thehref
attribute. It is an enumerated attribute. Each potential destination is a keyword for this attribute, mapping to a state of the same name. The attribute must be specified onlink
elements that have arel
attribute that contains thepreload
keyword. It may be specified onlink
elements that have arel
attribute that contains themodulepreload
keyword; in such cases it must have a value which is a script-like destination. For otherlink
elements, it must not be specified.
这篇关于`script`和`link as =" script"`标签之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!