最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Syntax to declare JS scripts - Stack Overflow

programmeradmin4浏览0评论

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!

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!

Share Improve this question asked Sep 22, 2008 at 16:22 LaylaLayla 4,2857 gold badges28 silver badges20 bronze badges 1
  • 1 As several posts have said, you can just use type, but the value should be entirely in lowercase: "text/javascript" – Prestaul Commented Sep 22, 2008 at 16:34
Add a ment  | 

7 Answers 7

Reset to default 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).

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

<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 ponents. There is no need to use the language or type attributes. It is the server, not the script tag, that determines the MIME type.

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.

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

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

Use both:

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

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 remend sticking to the latter. Your mileage may vary though!

发布评论

评论列表(0)

  1. 暂无评论