开发者

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

开发者 https://www.devze.com 2022-12-26 20:04 出处:网络
This question already has answers here: HTML Script tag: type or language (or omit both)? (3 answers)开发者_如何转开发
This question already has answers here: HTML Script tag: type or language (or omit both)? (3 answers)开发者_如何转开发 Closed 6 years ago.

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?


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


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.


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号