Type inference = "text / javascript" language = "javascript"

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

Now we had no problem in any of the main browsers with its tags. I am talking about all versions of IE, FF, Safari and Chrome.

Personally, I feel that it is laziness and just completely wrong and bad coding practice to leave such things even if they work without it.

Does anyone know if they should be included or only one or is it ok to leave both in ASP.NET?

+43
javascript html
Apr 13 '10 at 2:15
source share
6 answers

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

For a document such as HTML5, it is no longer required . For example: we leave this on the pages here in Stack Overflow. If you use the HTML type of the document, then it is completely optional and defaults to text/javascript , so you will completely stop it in any current (or even very old) browser. This was actually true in HTML4, but not strictly correct HTML.

For a document like HTML4 (valid) you need to . In order for the browser to really function, it is not strictly necessary and will behave perfectly (this was true until Netscape 2), but it will be invalid HTML. If you have HTML4 doctype, then hold it and act - because hey, why not?




Original answer:

I would use type="text/javascript" to be safe in all existing browsers, why leave ambiguity there to save 21 characters? language="" however outdated, I would not have it .

In addition, any validator is going to throw an error, although it will probably work inside the browser (if you are not dealing with something very old).

+70
Apr 13 '10 at 2:18
source share

According to the w3c spec , type is required. So ... although most browsers will be strong enough to work without specifying type correctly, it's a good practice to explicitly set it to text/javascript .

+15
Apr 13 '10 at 2:18
source share

W3C recommendation for HTML5 says you don't need to include

type="text/javascript"

The browser assumes that it is text / javascript, unless otherwise specified as another type.

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

+14
Jul 11 '11 at 23:10
source share

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

language="javascript"

This attribute is deprecated. This was used to select different programming languages ​​and specific JavaScript versions. You do not need it. do not use it.

type="text/javascript"

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

Your boss may do this for β€œright” or β€œwrong” reasons (i.e., he may follow Crockford's advice, or he may just be lazy), but I don’t think you can make a judgment. If the rest of its HTML and JS is messy, this is another thing. I would venture that the contents of the script tag may be more religious, such as the size of the tabs or the placement of brackets.

Edit: @coffeeaddict indicated that not putting the correct attributes in the tag would prevent it from compiling. I would say that trumps all the considerations about whether attributes are strictly correct or necessary, because projects should always build cleanly without errors or warnings. The same goes for validators, etc., if they are part of a standard project.

+10
Apr 13 '10 at 2:59 april
source share

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

If you use HTML 4.x or XHTML 1.x, the default scripting language is supposedly determined from the value of the Content-Script -Type header, whether it is present locally in the META / meta meta tag (high priority) or as an HTTP header (low priority). The type attribute still requires 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 such as onload, onclick, etc. are processed ., Type attribute with text / javascript, since the value should be used in the case of JavaScript instead of the language attribute if you do not use old browsers (for example, IE4, NN4, perhaps IE5 / Mac?), Remember that version IE6 for Windows 98, therefore, the language attribute is definitely out of date).

One last bit of information: technically speaking, application / x-javascript is the right value for JavaScript (unless it has become an application / javascript if I don't know), but unfortunately text / javascript is the one that has the most support in terms of multi-browser compatibility.

+8
Apr 13 2018-10-10T00:
source share

ASP.NET has nothing to do with it. xhtml 1.0 dictates that you use type = "text / javascript", with quotes, otherwise you will not create a valid xhmtl.

Run w3c validator on your pages and please abide by it.

+2
Apr 13 '10 at 2:19
source share



All Articles