tags:

views:

264

answers:

6

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?

+10  A: 

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.

Justin Ethier
+1 for quoting the W3C spec
George Edison
+12  A: 

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).

Nick Craver
No idea why this was down-voted; seems like sound advice.
Noldorin
@Noldorin - I suppose someone disagrees...it's a shame you aren't forced to leave a comment when down-voting, if an answer's wrong it'd be helpful to others to say *why*, oh well.
Nick Craver
@Noldorin: Agreed. So many people downvote without explanation. Such a shame. You get +1 from me, though.
George Edison
it's extremely annoying for example in Visual Studio that by default it warns you of the lack of proper mark-up. It's annoying NOT because of VS but because of developers who skip proper mark-up.
CoffeeAddict
@Nick: Yeah, it slightly annoys me too when people down-vote without leaving a comment (and there are no previous comments). However, if one were forced to leave a comment, I might anticipate a lot of nonsense ones/spam.
Noldorin
A: 

ASP.NET has nothing to do with anything. xhtml 1.0 dictates you use type="text/javascript", with quotes, or else you're not producing valid xhmtl.

Run the w3c validator against your pages, and please comply with what it asks.

Stefan Kendall
if you were to add runat="sever" it's possible asp.net could generate the proper xhtml, don't know, but his question about it isn't totally off.
Stephen Swensen
+3  A: 

If the document is parsed as HTML5, the language will default to JavaScript, and no attribute of any sort is required (for future reference, there is no language attribute in HTML5).

If you're catering to HTML 4.x or XHTML 1.x, the default scripting language is supposedly determined from the value of the Content-Script-Type header, whether present locally in a META/meta tag (high priority) or as an HTTP header (low priority). The type attribute is still required by HTML 4.x even if the Content-Script-Type header is present (locally or otherwise) since the default scripting language only affects how the values of attributes like onload, onclick, etc. are handled. The type attribute with "text/javascript" as the value ought to be used in the case of JavaScript instead of the language attribute unless you're catering to old browsers (e.g. IE4, NN4, perhaps IE5/Mac? ; remember that there was a version of IE6 for Windows 98, so the language attribute is definitely outdated enough).

One last bit of information: technically speaking, application/x-javascript is the correct value for JavaScript (unless it became application/javascript without me knowing), but unfortunately text/javascript is the one with the greatest support in terms of cross-browser compatibility.

Dustin
it is `application/javascript`, btw
Dan Beam
http://stackoverflow.com/questions/189850/what-is-the-javascript-mime-type-what-belongs-in-the-type-attribute-of-a-script
Dan Beam
+4  A: 

Douglas Crockford, one of the great authorities and teachers of Javascript, has this to say:

language="javascript"

This attribute has been deprecated. It was used to select other programming languages and specific versions of JavaScript. You don't need it. Don't use it.

type="text/javascript"

This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.

Your boss may be doing this for the "right" or the "wrong" reasons (i.e. he may be following Crockford's advice, or he may just be lazy), but I don't think you can necessarily make a judgement. If the rest of his HTML and JS is sloppy, that's another thing. I'd venture that the contents of the script tag could be more of a religious thing like tab size or brace placement.

Edit: @coffeeaddict has pointed out that not putting the proper attributes into the tag messes up his compiles. I'd say that trumps any consideration of whether the attributes are strictly correct or necessary, because projects should always build cleanly without errors or warnings. Same goes for validators, etc, if they are part of the project standard.

brainjam
It's not about judgement...well maybe it is! It's sloppy in my mind when developers do not take the time to put in proper attributes. You never know what could change or what one-off issues may happen. Also, as other people have stated it's annoying as hell for me to see warnings all over my compiler about improper mark-up. It doesn't make the life of other developers any easier when we leave out stuff that should be in there. We shouldn't depend on the "assumption" that all browsers are always going to save our asses. And I disagree that religion should override proper mark-up.
CoffeeAddict
One of the instances where Crockford is wrong. The current standard for HTML makes the type attribute mandatory. The drafts for HTML 5 make it optional.
David Dorward
A: 

You can also use a meta tag to specify the default scripting language, and omit it in the script tags.

<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
kennebec
This specifies the scripting language for intrinsic event attributes (e.g. `onclick`). It is *required* **iff** you use those attributes (but you should be following the principles of unobtrusive JavaScript and not be using them). It has no effect on script **elements** for which the type attribute is required in all current versions of HTML and XHTML. The attribute is optional in the HTML 5 draft.
David Dorward