DEV Community

Discussion on: Why hasn't the 'script' tag gotten a void version, yet? (E.g. 'extscript')

Collapse
abhinav1217 profile image
Abhinav Kulshreshtha • Edited on


<link href="js/script.js" rel="text/javascript" as="script" \>

This is kind of valid on all major browser. But here is a caveat, Script tags loads the scripts and executes it. Link tag links an external resource which 'may or maynot' contains thing you might be looking for. And as by the spec, rel and as attributes only acts as a guidance on what this linked resource might be.

Above is oversimplification. In a bit more technical term, There are 5 types of tags in HTML5, and only void and foreign type of tags are allowed to self close. Job of script tag is to contain script. Only reason you can share resource between different script tags on same page because of global prototype design of javascript. i.e everything is part Window.x.x.x.x.x namespace. As far as I am aware, it is impossible to create a separate scope outside the hierarchy of Window object in a browser. Node is not browser.

More on this.

  1. stackoverflow.com/questions/121784...
  2. stackoverflow.com/questions/511059...
  3. developer.mozilla.org/en-US/docs/W...
  4. html.spec.whatwg.org/multipage/syn...

P.S. Chrome based browsers does accept self-closing script because of the highly forgiving nature of web browsers, but it is not a w3c standard. This is just chrome cleaning your mess. Similarly to how firefox automatically adds <tbody> in the tables because we were too busy to do so.