11

For example here the code goes like this:

<html lang="en">
   <head>
     <!-- whatever -->
     <script>
       $(function() {
         $( "#datepicker" ).datepicker();
       });
     </script>
   </head>
 <!-- whatever -->

Note that <script> should have contained type attribute (perhaps set to "text/javascript") but it is not present here.

This is not the only example I've seen. Such code makes Visual Studio editor unhappy - it underlines <script> and says there should be a type attribute. It also makes me curious big time.

Why is type often omitted? What happens if I add type="text/javascript" - will jQuery break or something?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • did you try adding the type? – j0hnstew Mar 04 '13 at 14:42
  • 3
    most browsers default to javascript, but you always SHOULD specify the type anyways. JS may not always be the default. It's the buzzword compliant language du-jour, but tomorrow some other language may take over and become the defacto standard. – Marc B Mar 04 '13 at 14:42
  • @stewbydoo The question isn't how, it's why – Lee Taylor Mar 04 '13 at 14:42
  • if you're using a modern browser it will know the default is javascript but my co-worker had a simple problem where he forget to put the type and that broke everything. so just include it lol – j0hnstew Mar 04 '13 at 14:43
  • @LeeTaylor his last question is will jQuery break if you add it...only one way to find out...so try it – j0hnstew Mar 04 '13 at 14:44
  • I remember having a very similar (or related) doubt : http://stackoverflow.com/questions/9664282/difference-between-application-x-javascript-and-text-javascript-content-types and got some very good answers – Obmerk Kronen Mar 04 '13 at 14:45
  • 3
    You missed the first line of the example, ` ` very important. That changes to html5 rather than html4, making the type attribute not required. – Kevin B Mar 04 '13 at 15:15

5 Answers5

15

That's probably because HTML5 does not require a type attribute on <script> elements (it defaults to "text/javascript").

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 4
    "text/javascript" is default type for many-many years. Not for HTML5 only. – dfsq Mar 04 '13 at 14:42
  • 4
    @dfsq, well, the `type` attribute [was required in HTML4](http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.1), so you did have to explicitly specify it. – Frédéric Hamidi Mar 04 '13 at 14:45
  • 2
    That's right. Better to say, that browser will correctly use javascript as scripting language even if it's not specified. But if it's omitted code can be invalid if it's not HTML5 doctype. – dfsq Mar 04 '13 at 14:48
5

In HTML 5 it's now set by default that the script will be of type JavaScript, so there's no need to specify it. HTML 4.01 does require it though, it seems.

See here: http://www.w3.org/TR/html401/interact/scripts.html#h-18.2.1

And: http://www.w3.org/TR/html5/scripting-1.html#script

TJ Fogarty
  • 161
  • 1
  • 8
4

It is no longer a requirement in html5.

http://www.w3.org/html/wg/drafts/html/master/scripting-1.html

Dreamwalker
  • 3,032
  • 4
  • 30
  • 60
2

The type tag is optional. Default programming languages in all browsers has been JavaScript so that is what it defaults to. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out.

The browsers will know it is Javascript.

Walls
  • 3,972
  • 6
  • 37
  • 52
1

Visual Studio might be unhappy because it's lazy and needs to parse your code inside script tag and intellisense must work as well, so it's better when you provide that type directly instead of letting VS guess what language are you using, i.e. text/vbscript.

You could write some html page with advanced vb scripting under Visual Studio. http://www.w3schools.com/vbscript/default.asp.

Slawomir Brys
  • 341
  • 3
  • 4