Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaving out type="text/javascript" language="javascript" [duplicate]

Most of the script tags I create, I always include type="text/javascript" language="javascript" in the tag. My boss however does not. Sometimes he excludes both, sometimes just has language=javascript even without the quotes

Now we have not had an issue in any of the major browsers with his tags. I'm talking about all versions of IE, FF, Safari, and Chrome.

Personally I feel it's laziness and just totally improper and bad coding practice to leave stuff out like this even if it works without it.

Anyone know if both should be included or just one or is it ok to leave both out in ASP.NET?

like image 850
PositiveGuy Avatar asked Apr 13 '10 02:04

PositiveGuy


People also ask

Is type text/JavaScript necessary?

The type attribute in JavaScript is optional since the introduction of HTML5 brought some new improvements. JavaScript became the default language for HTML5 and modern browsers. So, now adding text/javascript isn't required in <script> tag.

What type of language is JavaScript?

What is JavaScript? JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it's used in many non-browser environments as well.

What is the problem with JavaScript?

These days, most cross-browser JavaScript problems are seen: When poor-quality browser-sniffing code, feature-detection code, and vendor prefix usage block browsers from running code they could otherwise use just fine. When developers make use of new/nascent JavaScript features, modern Web APIs, etc.)

What is the correct tag used to add JavaScript code in a HTML document?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute.


3 Answers

I suppose this should be updated now that the landscape has changed quite a bit:

For an HTML5 doctype, it's no longer required. For example: we leave it out of the pages here at Stack Overflow. If you're using an HTML5 doctype then it's completely optional and defaults to text/javascript, so you're absolutely fine leaving it off in every current (or even very old) browser. Realistically, this was also true even in HTML4 though not strictly valid HTML.

For an HTML4 doctype (to be valid), you need it. For a browser to actually function, it's not strictly needed and will behave just fine (this has been true all the way back to Netscape 2) - but it won't be valid HTML. If you have an HTML4 doctype, then keep it around and be valid - cause hey, why not?


Original Answer:

I would use type="text/javascript" to be safe in all current browsers, why leave the ambiguity in there to save 21 characters? language="" however is deprecated, I'd leave it out.

Also, any validator is going to throw an error, though it will likely work inside the browser (unless you're dealing with something very old).

like image 136
Nick Craver Avatar answered Oct 06 '22 23:10

Nick Craver


According to the w3c spec, type is required. So... even though most browsers are going to be robust enough to work without type being properly specified, it is good practice to explicitly set it to text/javascript.

like image 44
Justin Ethier Avatar answered Oct 06 '22 21:10

Justin Ethier


The W3C recommendation for HTML5 says that you do not need to include:

type="text/javascript"

The browser assumes that it is text/javascript unless otherwise stated as a different type.

http://dev.w3.org/html5/spec/Overview.html#the-script-block-s-type

like image 28
Jacob Avatar answered Oct 06 '22 21:10

Jacob