`script` 和 `link as="script"` 标签之间的区别
Difference between `script` and `link as="script"` tags
除加载脚本的标准方法外:
<script src="js/script.js"></script>
我见过有人这样做:
<link href="js/script.js" as="script">
有区别吗?
注意:有一个类似的 What's the difference between using link and script tag to reference JavaScript source? 问题询问 <link href="script.js" type="text/javascript" />
,这是不同的。
如果那个 link
标签上有 rel="preload"
(或 rel="modulepreload"
),它将表示一个预加载请求,它会预加载,但不会预加载 运行,脚本。相反,script
加载并 运行 脚本。但是如果没有 rel
,那 link
是无效的并且没有任何有用的效果(至少在规范方面)。
如果您在规范中查找 link
,您会看到 as
列为:
as
— Potential destination for a preload request (for rel="preload"
and rel="modulepreload"
)
在 link 到 as
属性之后,它说:
The as
attribute specifies the potential destination for a preload request for the resource given by the href
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 on link
elements that have a rel
attribute that contains the preload
keyword. It may be specified on link
elements that have a rel
attribute that contains the modulepreload
keyword; in such cases it must have a value which is a script-like destination. For other link
elements, it must not be specified.
除加载脚本的标准方法外:
<script src="js/script.js"></script>
我见过有人这样做:
<link href="js/script.js" as="script">
有区别吗?
注意:有一个类似的 What's the difference between using link and script tag to reference JavaScript source? 问题询问 <link href="script.js" type="text/javascript" />
,这是不同的。
如果那个 link
标签上有 rel="preload"
(或 rel="modulepreload"
),它将表示一个预加载请求,它会预加载,但不会预加载 运行,脚本。相反,script
加载并 运行 脚本。但是如果没有 rel
,那 link
是无效的并且没有任何有用的效果(至少在规范方面)。
如果您在规范中查找 link
,您会看到 as
列为:
as
— Potential destination for a preload request (forrel="preload"
andrel="modulepreload"
)
在 link 到 as
属性之后,它说:
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.