Can I load javascript code using <link> tag?

前端 未结 8 1511
情歌与酒
情歌与酒 2020-12-02 20:20

Can I load javascript code using tag in my website ?

For example I have a javascript file, test.js, which contains the simple

8条回答
  •  暖寄归人
    2020-12-02 20:51

    Possible rationale why not

    • link elements are only allowed where "Metadata content" is allowed, typically head, and not in the body. See: Contexts in which this element can be used. All embedded elements that go in the body have separate elements for them: img, iframe, etc.

    • link elements must be empty, and script may be non-empty. See: Content model

    Therefore it is natural to have a separate element for JavaScript, and since we must have a separate element, it is better not to duplicate functionality with link rel="script".

    This theory also explains why img and style have separate elements:

    • img can be placed in the body, so it gets a separate element, even though it must be empty.

    • style can be non-empty, so it gets a separate element, even though until HTML5 it could not be placed in the body (now it can via scoped, but still not to include external scripts).

提交回复
热议问题