Best way to link to external scripts in Document HEAD
-
06-09-2019 - |
Question
I've noticed many sites use this, without closing the tag.
<script type="text/javascript" src="editor.js">
This style is also recommended but is longer:
<script type="text/javascript" src="editor.js"></script>
Can I write it like this? Is it valid or is there a better way?
<script type="text/javascript" src="editor.js" />
Solution
You always want to use
<script type="text/javascript" src="editor.js"></script>
Some browsers do not allow self-closing script tags.
For more information, see Why don't self-closing script tags work?
OTHER TIPS
Use the second option. Not all browsers support the self-closing style.
<script type="text/javascript" src="editor.js">
This is invalid, and will break things.
<script type="text/javascript" src="editor.js"></script>
This is fine.
<script type="text/javascript" src="editor.js" />
This is fine so long as you are using XHTML that is not HTML Compatible. This means you need to serve the XHTML with an XML content-type (preferably application/xhtml+xml) and forget about supporting Internet Explorer (except with a separate document).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow