44

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?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
  • I only use type, I'm not aware of any modern specs which define language so it's redundant. I can't remember exactly, but I do recall getting into trouble once omitting both type and language (maybe visual studio thought it was vbscript). Also, whether or not you use quotes around any attribute values is a matter of whether you are using a strict xhtml 1 doctype. – Stephen Swensen Apr 13 '10 at 02:31
  • See answer here: http://stackoverflow.com/a/4195441/260080 – Marco Demaio Dec 14 '12 at 13:10

6 Answers6

72

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

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 9
    No idea why this was down-voted; seems like sound advice. – Noldorin Apr 13 '10 at 02:21
  • 5
    @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 Apr 13 '10 at 02:23
  • 2
    @Noldorin: Agreed. So many people downvote without explanation. Such a shame. You get +1 from me, though. – Nathan Osman Apr 13 '10 at 03:17
  • 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. – PositiveGuy Apr 13 '10 at 03:44
  • 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 Apr 13 '10 at 14:27
  • 1
    22 chars + space. Agreed on -1 => comment, maybe the author could be let blank for the mere mortals (seen by admins and gods). Discussion for *meta* – Déjà vu Oct 06 '12 at 07:48
  • 1
    Sorry... a bit of resurrecting the dead here. I know this question is 4 years old now, but I just wanted to note that in HTML 5 the type="" attribute is no longer required and defaults to "text/javascript": http://www.w3.org/html/wg/drafts/html/master/scripting-1.html#attr-script-type Also wanted to note that "text/javascript" is technically obsolete and that "application/javascript" is the modern equivalent: http://en.wikipedia.org/wiki/Internet_media_type – Moismyname May 12 '14 at 19:07
15

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
  • 131,333
  • 52
  • 229
  • 284
14

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

Jacob
  • 3,835
  • 8
  • 25
  • 25
11

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
  • 18,863
  • 8
  • 57
  • 82
  • 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. – PositiveGuy Apr 13 '10 at 03:41
  • 4
    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. – Quentin Apr 13 '10 at 13:17
  • I don't know if it is lazy; I mean, why type the extra characters if they aren't necessary and they waste some bandwidth? The html also looks cleaner. Also, we are having to update sites all the time no matter what, to say that MAYBE it will change some day and it really will matter seems mute to me---the web will almost CERTAINLY change in some expected way, which will make you update the site in much more drastic ways than adding in an optional attribute. If this ever broke your site, it would take 15 minutes to refactor and add it in... seems low on the "risk management" totem pole. – jdg Jan 18 '14 at 19:38
8

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
  • 1,956
  • 14
  • 14
2

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
  • 66,414
  • 68
  • 253
  • 406
  • 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 Apr 13 '10 at 02:35
  • +1 for "run the w3c validator and please comply with what it asks". Valid HTML pages works better than not valid ones, at least during development. WRC validator always saved me a good quantity of time during development of a page, instead of running after the reason of why some DIV block is not showing or not showing in the proper place and accusing CSS or browser rendering when it's only a HTML mistake that the validator would immediately spot. Useless to tell you there is a good FF extension that let's you validate page istantly: https://addons.mozilla.org/it/firefox/addon/html-validator/ – Marco Demaio Dec 14 '12 at 13:00