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.
- @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
1 Answer
Reset to default 14If 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 (forrel="preload"
andrel="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 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.