1

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?

2
  • 4
    text/javascript is the default type, so you can omit it, it makes no difference. Commented Sep 9, 2014 at 6:03
  • Nothing, except in very special cases with old IE where if the first script element set the type to some other scripting language (e.g. VBScript) then that was the default if no type was specified. In practice, it's always been safe to omit the type, but it wasn't valid until HTML5. Commented Sep 9, 2014 at 6:04

1 Answer 1

3

<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:

  1. If you are using HTML5, just use <script>.
  2. If you are using anything older, use <script type="text/javascript">.
  3. 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.

have a look at this site for more detail

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.