javascript 解決瀏覽器不支援的問題_javascript技巧

來源:互聯網
上載者:User

為了程式/代碼的易讀性,基本上每一種程式設計語言都有注釋的功能,javascript也不例外,javascript注釋代碼有多種形式,本文章向大家介紹javascript注釋代碼的兩種方法,

javascript注釋代碼一般有兩種方法:

單行注釋
多行注釋 

javascript單行注釋

單行注釋以"//"開頭,到改行的末尾結束。下面是javascript單行注釋執行個體:

<html><head><title>javascript單行注釋</title><script language="javascript"><!--// The first alert is belowalert("An alert triggered by JavaScript!");// Here is the second alertalert("A second message appears!");// --></script></head><body></body></html>

 javascript多行注釋

多行注釋以 /* 開始,以 */ 結尾。下面的例子使用多行注釋來解釋代碼:

<html><head><title>javascript多行注釋</title><script language="javascript"><!--/*Below two alert() methods are used tofire up two message boxes - note how thesecond one fires after the OK button on thefirst has been clicked*/alert("An alert triggered by JavaScript!");alert("A second message appears!");// --></script></head><body></body></html>

 如何解決瀏覽器不支援javascript

應用注釋符號驗證瀏覽器是否支援JavaScript指令碼功能.

如果使用者不能確定瀏覽器是否支援JavaScript指令碼,那麼可以應用HTML提供的注釋符號進行驗證。HTML注釋符號是以“<--”開始以“-->”結束的。如果在此注釋符號內編寫JavaScript指令碼,對於不支援JavaScript的瀏覽器,將會把編寫的JavaScript指令碼作為注釋處理。

使用JavaScript指令碼在頁面中輸出一個字串,將JavaScript指令碼編寫在HTML注釋中,如果瀏覽器支援JavaScript將輸出此字串,如果不支援將不輸出此字串,代碼如下:

<html><head><title>Hide scripts using comments.</title><script language="javascript" type="text/javascript"><!--document.write("您的瀏覽器支援JavaScript指令碼!");//--></script></head><body>Page content here...</body></html>

通過此文希望能協助到有需要的朋友,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.