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

javascript - What is the difference between `script` and `link as="script"` tags? - Stack Overflow

programmeradmin2浏览0评论

Additionally to the standard method of loading scripts:

<script src="js/script.js"></script>

I have seen people do this:

<link href="js/script.js" as="script"> 

Is there any difference?


Note: There's a similar What's the difference between using link and script tag to reference JavaScript source? question asking about <link href="script.js" type="text/javascript" />, which is different.

Additionally to the standard method of loading scripts:

<script src="js/script.js"></script>

I have seen people do this:

<link href="js/script.js" as="script"> 

Is there any difference?


Note: There's a similar What's the difference between using link and script tag to reference JavaScript source? question asking about <link href="script.js" type="text/javascript" />, which is different.

Share Improve this question edited Sep 3, 2022 at 16:14 leonheess asked Jun 29, 2018 at 16:26 leonheessleonheess 21.5k19 gold badges94 silver badges136 bronze badges 2
  • @Rustyjim I found this, too, but it is an old question that came to the, at this time, correct solution that link cannot be used to load javascript which does no longer seem to be the case – leonheess Commented Jun 29, 2018 at 16:34
  • 1 Well i also found this w3c.github.io/preload/#link-element-extensions maybe helpful :) – UnpassableWizard Commented Jun 29, 2018 at 16:43
Add a ment  | 

1 Answer 1

Reset to default 14

If that link tag had rel="preload" (or rel="modulepreload") on it, it would indicate a preload request, which would preload, but not run, the script. In contrast, 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).

If you look up link in the spec, you'll see as listed as:

as — Potential destination for a preload request (for rel="preload" and rel="modulepreload")

Following the link to as attribute, it says:

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.

发布评论

评论列表(0)

  1. 暂无评论