Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I include type="text/javascript" in my SCRIPT tags?

Tags:

javascript

I read through Crockford's JavaScript best practise, and he stated:

There is no need to use the language or type attributes. It is the server, not the script tag, that determines the MIME type.

But I've never seen anyone omit the type attribute in their <script> tag...

Do you guys include type? Why (not)?

like image 297
peirix Avatar asked Jun 12 '09 07:06

peirix


1 Answers

You misunderstood what Crockford meant, he didn't say the type attribute is completely invalid, it's just that it's incorrect. The MIME type for JavaScript is application/javascript (or application/ecmascript I can't remember right now).

The general usage though is that is text/javascript which browsers will handle without any problems as that's been the defacto standard for a long time.

In regards to the <script src="..." tag it is redundant because the server determines the MIME type of the file and that is what the browser will then deal with.

He best explains it in one of his videos on YUI Theater (http://developer.yahoo.com/yui/theater/). I don't remember exactly which one he talks about this, I think it was in the advanced JavaScript series (but yeah I've watched them all a few times so they kind of blur into each other).

So if you want to write valid XHTML you need to provide something like text/javascript but it's not the official MIME type of the JavaScript language.

like image 81
Aaron Powell Avatar answered Oct 01 '22 12:10

Aaron Powell