`script`和`link as =" script"`标签之间的区别 [英] Difference between `script` and `link as="script"` tags

查看:272
本文介绍了`script`和`link as =" script"`标签之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了加载脚本的标准方法之外:

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 (for rel="preload" and rel="modulepreload")

关注 as 属性,它说:

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 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`和`link as =&quot; script&quot;`标签之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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