102

I was just wondering, what is the difference between

<script>

and

<script type = 'text/javascript'>

Is it different for different webservers?

For example,(I know it's incorrect to provide a link from w3schools, but look)

http://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst

Using chrome, I visited w3schools and I realised that the <script> tag is all I need.

However, when I did an offline javascript test, i realised that i need the

<script type = 'text/javascript'>

tag. Why is this so?

3
  • @Pumbaa80 My question is different. I asked if it is affected by different web servers
    – Lok Jun
    Commented Dec 25, 2013 at 10:48
  • 2
    It is a duplicate, and this does not depend on the web server. If you think you need the attribute in an offline test, then you should present ask about that specifically and provide code that demonstrates what you mean (and explain why you think you “need” it). Commented Dec 25, 2013 at 15:02
  • 1
    @JukkaK.Korpela nope. I specified my own problem too.
    – Lok Jun
    Commented Dec 26, 2013 at 9:47

5 Answers 5

92

In HTML 4, the type attribute is required. In my experience, all browsers will default to text/javascript if it is absent, but that behaviour is not defined anywhere. While you can in theory leave it out and assume it will be interpreted as JavaScript, it's invalid HTML, so why not add it.

In HTML 5, the type attribute is optional and defaults to text/javascript

Use <script type="text/javascript"> or simply <script> (if omitted, the type is the same). Do not use <script language="JavaScript">; the language attribute is deprecated

Ref :
http://social.msdn.microsoft.com/Forums/vstudio/en-US/65aaf5f3-09db-4f7e-a32d-d53e9720ad4c/script-languagejavascript-or-script-typetextjavascript-?forum=netfxjscript
and
Difference between <script> tag with type and <script> without type?

Do you need type attribute at all?

I am using HTML5- No

I am not using HTML5 - Yes

14
  • But why doesn't w3schools try-it-editor require the whole line?
    – Lok Jun
    Commented Dec 25, 2013 at 9:52
  • Are you quoting that text from something? Commented Dec 25, 2013 at 9:53
  • 9
    @dholakiyaankit Please quote the source as well. Now people might think that, these are your own words. Commented Dec 25, 2013 at 9:55
  • Can you tell me where you got that from?
    – Lok Jun
    Commented Dec 25, 2013 at 9:55
  • 1
    @LuizaRodrigues text/javascript is obsolete always use application/javascript when you want to. stackoverflow.com/a/21098951/2630817
    – Just code
    Commented Aug 5, 2020 at 2:54
30

<script> is HTML 5.

<script type='text/javascript'> is HTML 4.x (and XHTML 1.x).

<script language="javascript"> is HTML 3.2.

Is it different for different webservers?

No.

when I did an offline javascript test, i realised that i need the <script type = 'text/javascript'> tag.

That isn't the case. Something else must have been wrong with your test case.

0
13

Douglas Crockford says:

type="text/javascript"

This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.

In HTML 4.01 and XHTML 1(.1), the type attribute for <script> elements is required.

5
  • Where did you get that from?
    – Lok Jun
    Commented Dec 25, 2013 at 9:54
  • @leonneo — There's a link in the answer.
    – Quentin
    Commented Dec 25, 2013 at 9:55
  • @leonneo taken from javascript.crockford.com/script.html as added link at top. Commented Dec 25, 2013 at 9:56
  • 4
    Just to clarify: Crockford is talking about making it work, as opposed to making it valid. Commented Dec 26, 2013 at 10:46
  • 1
    You had me at Douglas.
    – WynandB
    Commented Feb 21, 2014 at 3:15
6
<!-- HTML4 and (x)HTML -->
<script type="text/javascript"></script>


<!-- HTML5 -->
<script></script>

type attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. If this attribute is absent, the script is treated as JavaScript.

Ref: https://developer.mozilla.org/en/docs/Web/HTML/Element/script

0
-4

You only need <script></script> Tag that's it. <script type="text/javascript"></script> is not a valid HTML tag, so for best SEO practice use <script></script>

Not the answer you're looking for? Browse other questions tagged or ask your own question.