JavaScript 基礎問答三

來源:互聯網
上載者:User

9. JavaScript的注釋(Comments in JavaScript)
Q:我如何在JavaScript代碼中插入注釋?
A:JavaScript支援三種不同類型的注釋:
多行C樣式的注釋。 包括在/* 和 */ 內的內容都是注釋,例如:
/* This is a comment */
/* C-style comments can span
as many lines as you like,
as shown in this example */
C++樣式的單行注釋。這些注釋以//開始,到行尾結束:
// This is a one-line comment
使用HTML注釋開始序列(<!--)引導的單行注釋。注意,JavaScript解譯器會忽略HTML注釋的結束字元(-->)。考慮這個執行個體:
<!-- This is treated as a one-line JS comment
<!-- It works just like a comment beginning with //
<!-- --> This is also a one-line JS comment
<!-- --> because JS ignores the closing characters
<!-- --> of HTML-style comments
HTML樣式的注釋在JavaScript代碼中並不常見。(//引導的單行注釋更簡單也更易讀)。不過,強烈建議使用HTML注釋將JavaScript代碼從舊版本瀏覽器中隱藏。
10. 從舊版瀏覽器中隱藏JS代碼 (Hidding JS code from old browsers)
Q:我如何從不支援JavaScript的舊版中隱藏JS代碼?
A:為了阻止舊版瀏覽器顯示JS代碼,可以使用以下方法:
在開頭的<script>標籤後,立即放入一行HTML樣式的注釋開頭樣式,沒有結尾內容,如此,你的指令碼的頭兩行看起來應該像:
<script language="JavaScript">
<!--
在指令碼結尾,放入下列兩行:
//-->
</script>
如此,你的HTML檔案就會包含下面的代碼端:
<script language="JavaScript">
<!--
Here you put your JS code.
Old browsers will treat it
as an HTML comment.
//-->
</script>
舊版本瀏覽器就會把JS代碼看做一個長的HTML注釋。另一方面,支援JavaScript的瀏覽器會正常解釋<script>和</script>之間的JS代碼(解譯器會將JS代碼中的第一行和最後一行當做單行注釋)。
11. JavaScript可用嗎?(Is JavaScript enabled?)
Q:如何使用者的瀏覽器不能執行JavaScript代碼,我可以給使用者一個警告嗎?
A:當然,你可以給那些使用不相容JavaScript瀏覽器的使用者顯示一個特定警告。把你的警告資訊放在<NOSCRIPT>和</NOSCRIPT>之間。這是一個樣本:
<NOSCRIPT>
<H3>This page uses JavaScript</H3>
<ul>
<li>Please use Netscape Navigator 3+ or Internet Explorer 3+
<li>Make sure that JavaScript is enabled in your browser.
</ul>
</NOSCRIPT>
支援JavaScript的瀏覽器會忽略<NOSCRIPT>和</NOSCRIPT>之間的內容。不能執行JavaScript的瀏覽器就會在頁面上顯示資訊。
注意:Netscape Navigator 2不支援<NOSCRIPT>。因此,這個訊息對那些即使使用支援JavaScript的瀏覽器Netscape 2使用者也是可見的。記住這一點,然後合理措詞,不用誤導了Netscape 2的使用者。(例如,上面資訊假設了你的腳步要求瀏覽器的版本至少為3。)
12. 外部JavaScript檔案(External JS Files)
Q:我可以從外部引入JS檔案,而不是把所有的指令碼都在HTML頁面中嗎?
A:可以。你可以建立一個尾碼名為.js的檔案,例如,myscipt.js。把你的JavaScript代碼放在這個檔案中;不要在.js檔案中包含<script>或</script>標籤。
要把myscript.js嵌入到網頁中,需要在HTML檔案中使用這些標籤:
<SCRIPT LANGUAGE="JavaScript" SRC="myscript.js">
</SCRIPT>
從實際上講,你會發現建立.js檔案儲存JavaScript函數是非常方便的,你可以在不同的HTML檔案中重用這些函數。然而,請注意,一些舊版本瀏覽器(像 Navigator 2.x 和Explorer 3.x)不會載入外部的JavaScript檔案。
13. JavaScript中的保留字(Reserved words in JavaScript)
Q:JavaScript中有哪些保留字呢?
A:JavaScript語言的保留字都列在了下面。(其中一些在JavaScript語言中使用,而在JavaScript保留是為了相容或者以後可能的擴充。)當選擇JavaScript變數名時,要避免這些保留字!
abstract else instanceof switch
boolean enum int synchronized
break export interface this
byte extends long throw
case false native throws
catch final new transient
char finally null true
class float package try
const for private typeof
continue function protected var
debugger goto public void
default if return volatile
delete implements short while
do import static with
double in super
除了以上的保留字,最好也避免以下標識符作為JavaScript變數名。這些是Netscape Navigator或者Internet Explorer中用戶端對象、方法或者屬性的名字。
alert hidden outerWidth
all history packages
anchor image pageXOffset
anchors images pageYOffset
area isNaN parent
array java parseFloat
assign JavaArray parseInt
blur JavaClass password
button JavaObject pkcs11
checkbox JavaPackage plugin
clearTimeout innerHeight prompt
clientInformation innerWidth prototype
close layer radio
closed layers reset
confirm length screenX
crypto link screenY
date location scroll
defaultStatus Math secure
document mimeTypes select
element name self
elements navigate setTimeout
embed navigator status
embeds netscape String
escape Number submit
eval Object sun
event offscreenBuffering taint
fileUpload onblur text
focus onerror textarea
form onfocus top
forms onload toString
frame onunload unescape
frames open untaint
frameRate opener valueOf
function option window
getClass outerHeight
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.