Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I generally include JavaScript files using the script tag as below.

<script type="text/javascript" src="somefile.js"></script>

I have seen some people using the language attribute as well.

Now-a-days I find many people omitting the type attribute. I have started to get a feeling that if JavaScript is the default scripting language then even I should omit the type attribute. Would it be good to omit the type attribute? Would it cause any problems?

share|improve this question

1 Answer 1

up vote 26 down vote accepted

Take a look at this as a reference (Book of Speed): http://www.bookofspeed.com/chapter3.html

Essentially the best way is to combine all your javascript into one file called something like all.min.js that is also minimized.

Typically in HTML5 you would do something like:

<script src="js/all.min.js"></script>

As you can see, you DO NOT need the type attribute in HTML5, but you do in other versions of HTML and XHTML (http://dev.w3.org/html5/spec-author-view/the-script-element.html). The spec clarifies that if the content is other than "text/javascript" then you need to specify the type attribute, in HTML5.

Some things to remember:

  • Always include it before the closing tag. (http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/)
  • You should load your scripts in a non-blocking pattern (http://labjs.com/)

Note:

If you are going to specify another type other than "text/javascript" you would use one of the following:

  • "application/ecmascript"
  • "application/javascript"
  • "application/x-ecmascript"
  • "application/x-javascript"
  • "text/ecmascript" "text/javascript"
  • "text/javascript1.0"
  • "text/javascript1.1"
  • "text/javascript1.2"
  • "text/javascript1.3"
  • "text/javascript1.4"
  • "text/javascript1.5"
  • "text/jscript"
  • "text/livescript"
  • "text/x-ecmascript"
  • "text/x-javascript"
  • "text/javascript;e4x=1"

The above list is from: http://dev.w3.org/html5/spec-author-view/the-script-element.html#scriptingLanguages

Remember that you would not use the language attribute, only the type attribute.

share|improve this answer
1  
And what about the language attribute. Is it good to specify it or omit it? –  Cracker Jul 9 '11 at 19:41
1  
The language attribute is not supported in HTML5, instead they have merged it with the type attribute. Again, if it is something other than "text/javascript" then you would specify it using the type attribute and one of the following from this page:dev.w3.org/html5/spec-author-view/the-script-element.html –  alvincrespo Jul 9 '11 at 19:47
    
I've updated the answer to reflect my comment above. –  alvincrespo Jul 9 '11 at 19:51
    
Does setting something like text/javascript1.3 have any actual effect, different than omitting it? –  Arturo Torres Sánchez Mar 2 at 15:36

protected by gnat Mar 2 at 14:30

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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