views:

389

answers:

7

Hi!
I'm not sure about what's the difference between opening a JS script with

<SCRIPT language='JavaScript'>

or with:

<SCRIPT type="text/JavaScript">

Should JavaScript always be quoted (either with " " or with ' ') or that's not really important?

Thank you for any clarification on this topic!

A: 

Use both:

<script language="javascript" type="text/javascript">
Joel Coehoorn
what's the difference between them?
Layla
Using the language attribute in a Strict document is incorrect. Use only type.
Jim
+16  A: 

The language attribute was used in HTML 3.2. HTML 4.0 introduced type (which is consistent with other elements that refer to external media, such as <style>) and made it required. It also deprecated language.

Use type. Do not use language.

In HTML (and XHTML), there is no difference between attribute values delimited using single or double quotes (except that you can't use the character used to delimit the value inside the value without representing it with an entity).

David Dorward
A: 

You should always enclose attribute values in quotation marks ("). Don't use apostraphes (').

Edit: Made opinion sound like fact here, my bad. Single quotes are technically legal, but in my experience they tend to lead to more issues than double quotes (they tend to crop up in attribute values more often amongst other things) so I always recommend sticking to the latter. Your mileage may vary though!

Luke Bennett
why? What is the reason
redsquare
Editted post for clarification
Luke Bennett
+1  A: 

Older browsers only support language - now the type method using a mimetype of text/javascript is the correct way.

<script language="javascript" type="text/javascript">

is used to support older browsers as well as using the correct way.

<style type="text/css">

is another example of including something (stylesheet) using the correct standard.

Rich Bradshaw
By older browsers you mean browsers that do not support html4. You should not consider such browsers, except in very special cases.
Marius
Yeah, I agree - coming back to this now I know a little more I wouldn't specify language. In fact, using HTML5, I wouldn't even specify type, as it's assumed to be text/javascript.
Rich Bradshaw
A: 

According to the W3 HTML 4.01 reference, only type attribute is required. The langage attribute is not part of the reference, but I think it comes from earlier days, when Microsoft fought against Netscape.

Also, simple quotes are not valid in XHTML 1.0 (the parsing is more restrictive). This may not be a problem but you should now that's always better to validate your html (either HTML 4.01 or XHTML 1.0).

paulgreg
"Simple quotes are not valid in XHTML" - this is not true. You must always quote attribute values, but you can use either single or double quotes.
Jim
+2  A: 

Refer to supreme deity Douglas Crockford's Javascript Code Conventions for all things Javascript:

JavaScript Files

JavaScript programs should be stored in and delivered as .js files.

JavaScript code should not be embedded in HTML files unless the code is specific to a single session. Code in HTML adds significantly to pageweight with no opportunity for mitigation by caching and compression.

<script src=filename.js> tags should be placed as late in the body as possible. This reduces the effects of delays imposed by script loading on other page components. There is no need to use the language or type attributes. It is the server, not the script tag, that determines the MIME type.

Rahul
The "supreme deity" is telling you to write invalid HTML for no good reason. The specifications are the authority here, not a person.
Jim
There are no specifications, only recommendations, and as such they are not the Whole Truth. And besides, who cares about valid HTML? Use what works. Douglas Crockford knows what works.
Rahul
I think you are confused about what a specification is. The recommendations published by the W3C *are* specifications. Take a look at the HTML 4.01 recommendation, for example. What's the title? "HTML 4.01 Specification".
Jim
Who cares about valid HTML? Using a validator is a great way of finding mistakes and can save a lot of time when you are trying to debug something. If you have dozens of errors that you think don't matter, it is more difficult to spot the ones that you think *do* matter.
Jim
And finally, please stop being such a Crockford fanboy and think for yourself.
Jim
I *am* thinking for myself, which is why I'm not strung up in the fascinating little world of W3C masturbation like you are, and instead working on doing what works. You can be an arrogant prick about it as much as you want, but it doesn't make you any more right. :)
Rahul
How are you thinking for yourself by blindly saying "this is what Mr Crockford thinks, so do that"? The HTML specifications define what is correct and incorrect HTML, not Douglas Almighty Crockford. That's not "W3C masturbation", that's common sense.
Jim
I'm saying HTML correctness isn't relevant. What's relevant is determining what's necessary for a script tag, and Crockford offers a compelling argument. I came to this conclusion by not blindly following the W3C, but instead looking around the web to find insightful points. This is one of them.
Rahul
HTML correctness sure is relevant when somebody is asking what the correct HTML is."Crockford offers a compelling argument", does he? What argument would that be? You have failed to notice that the document you link to offers *zero* reasons to omit the type attribute. Your deity just tells you to.
Jim
What are you talking about? He clearly says "It is the server, not the script tag, that determines the MIME type.". Hence there is no reason to simply declare type or language in the HTML just because the W3C says so, since it doesn't make a difference. It's up to the server.
Rahul
He did not offer any argument for omitting it, he merely said that it didn't matter in one particular way. And I already described one way in which it does makes a difference. Using invalid HTML for no good reason is silly. You have yet to suggest any reason at all, let alone a good one.
Jim
I agree with Rahul. On heavily visited websites, every byte counts. Bandwidth is not cheap, when 100.000 users loads [n] number of pages, with 22 bytes overhead (type="text/javascript").
roosteronacid
In essence: be pragmatic as long as nothing breaks. I couldn't care less about how many HTML-warnings my website has. As long as it renders correctly across all A-grade browsers.
roosteronacid
+1  A: 

You don't need the type and language attribute when using to an external JavaScript file:

<script src="script.js" />

Your browser will automatically figure out what to do, based on the extension of the file. You need type="text/javascript" when doing script-blocks, though.

Edit:

Some might say that this is awful, but these are in fact the words of a Yahoo! JavaScript evangelist (I think it was Douglas Crockford) in the context of website load-performance.

Perhaps I should have elaborated a bit.

Google was a great example of breaking standards without breaking the rendering of their website. (They are now complying to W3C standards, using JavaScript to render their pages). Because of the heavy load on their websites, they decided to strip down their markup to the bare minimum, and use depreciated tags like the dreaded font and i tags.

It doesn't hurt to be pragmatic. Within reason, of course :)

roosteronacid