3

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!

1
  • 1
    As several posts have said, you can just use type, but the value should be entirely in lowercase: "text/javascript" Commented Sep 22, 2008 at 16:34

7 Answers 7

16

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

13 Comments

The "supreme deity" is telling you to write invalid HTML for no good reason. The specifications are the authority here, not a person.
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.
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".
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.
And finally, please stop being such a Crockford fanboy and think for yourself.
|
1

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.

2 Comments

By older browsers you mean browsers that do not support html4. You should not consider such browsers, except in very special cases.
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.
1

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

Comments

0

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

1 Comment

"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.
-2

Use both:

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

2 Comments

what's the difference between them?
Using the language attribute in a Strict document is incorrect. Use only type.
-3

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!

1 Comment

Editted post for clarification

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.