JS判斷瀏覽器之Navigator對象_基礎知識

來源:互聯網
上載者:User
The Navigator Object
導航對象
The JavaScript Navigator object contains all information about the visitor's browser. We are going to look at two properties of the Navigator object:
JS導航對象包含所有有關訪問這者瀏覽器的資訊。我們將看看兩個導航對象的產物:

appName -  holds the name of the browser
appName - 含瀏覽器的名稱 
appVersion - holds, among other things, the version of the browser
appVersion - 瀏覽器版本 
Example
舉例 
<html><body> <script type="text/javascript"> var browser=navigator.appName var b_version=navigator.appVersion var version=parseFloat(b_version)document.write("Browser name: "+ browser) document.write("") document.write("Browser version: "+ version) </script> </body></html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

The variable browser in the example above holds the name of the browser, i.e. "Netscape" or "Microsoft Internet Explorer".
上面例子中變數brower(瀏覽器)被賦加了瀏覽器的名稱,網景或是IE(或其他)

The appVersion property in the example above returns a string that contains much more information than just the version number, but for now we are only interested in the version number. To pull the version number out of the string we are using a function called parseFloat(), which pulls the first thing that looks like a decimal number out of a string and returns it.
上面例子中的appVersion屬性返回一串包含比版本號碼更多的資訊字元,但現在我們只要版本號碼。要從字串中提出版本號碼我們使用一個叫parseFloat()的函數來返回數字。

IMPORTANT! The version number is WRONG in IE 5.0 or later! Microsoft start the appVersion string with the numbers 4.0. in IE 5.0 and IE 6.0!!! Why did they do that??? However, JavaScript is the same in IE6, IE5 and IE4, so for most scripts it is ok.
(有關JS返回IE版本號碼的問題)

Example
舉例
The script below displays a different alert, depending on the visitor's browser:
根據訪問者的瀏覽器,(不同的瀏覽器)下面的指令碼將顯示不同的警示:

<html> <head> <script type="text/javascript"> function detectBrowser(){var browser=navigator.appName var b_version=navigator.appVersion var version=parseFloat(b_version) if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4)) {alert("Your browser is good enough!")} else {alert("It's time to upgrade your browser!")}} </script> </head><body onload="detectBrowser()"> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

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