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.
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 (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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With