If I use just <script>, with JavaScript after, followed by </script>, inside an HTML document, my JavaScript loads just fine in the browser. I see some examples on-line use <script type='text/javascript'>, followed by </script>, though. Are there any advantages or differences to using these? Is my code incorrect if I am just using <script> around JavaScript?
1 Answer
<script></script> is introduced in html5. (This wouldn't work for older browser.)
In which we can omit the type as it uses the default type text/javascript
But before html5 we used <script type="text/javascript"></script>. And there was no default type specified.
So, to consider:
- If you are using HTML5, just use
<script>. - If you are using anything older, use
<script type="text/javascript">. - If you are writing scripts for people to use on their own sites
(e.g. copy-pasteable code, WordPress plugins, etc, use
<script type="text/javascript"> and CDATA.
1 Comment
t.niese
Even for older browsers
<script> should work, as long as the doctype does not enforce xhtml and only js is used. The specs before html5 required to use the type but the browsers always assumed javascript as default if it was not present. At least I don't know any browser after IE4/NS4, that would not behave that way in this case.
text/javascriptis the default type, so you can omit it, it makes no difference.