Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do jQuery samples often omit script type?

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?

like image 467
sharptooth Avatar asked Mar 04 '13 14:03

sharptooth


4 Answers

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

like image 130
Frédéric Hamidi Avatar answered Oct 19 '22 08:10

Frédéric Hamidi


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

like image 22
TJ Fogarty Avatar answered Oct 19 '22 07:10

TJ Fogarty


It is no longer a requirement in html5.

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

like image 42
Dreamwalker Avatar answered Oct 19 '22 07:10

Dreamwalker


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.

like image 24
Walls Avatar answered Oct 19 '22 08:10

Walls