当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS Browser

JavaScript
JS 介绍
JS 怎样使用
JS 在哪使用
JS 变量
JS If...Else
JS Switch
JS 操作符
JS Popup Boxes
JS 函数
JS For 循环
JS While 循环
JS Break 循环
JS For...In
JS 事件
JS Try...Catch
JS Throw
JS onerror
JS 特殊字符
JS Guidelines
JS 对象介绍

JavaScript 中的 JS Browser


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 661 ::
收藏到网摘: n/a

The JavaScript Navigator object contains information about the visitor's browser.
JS的导航对象包含了有关访问者浏览器的信息


Examples
举例

Detect the visitor's browser and browser version
判断访问者的浏览器(类型)以及版本

More details about the visitor's browser
更多有关访问者浏览器(详细信息)

All details about the visitor's browser
所有信息

Alert user, depending on browser
依据浏览器来提示用户


Browser Detection
浏览器的判别

Almost everything in this tutorial works on all JavaScript-enabled browsers. However, there are some things that just don't work on certain browsers - specially on older browsers.
在这个教程中的所有(JS例子)都能正常的在支持JS的浏览器上运行。然而,有些游览器就不行了- 特别是一些老的浏览器

So, sometimes it can be very useful to detect the visitor's browser type and version, and then serve up the appropriate information.
所以,一些时候判断访问者的浏览器类型以及版本并提供相适应的信息就来的非常有用了。

The best way to do this is to make your web pages smart enough to look one way to some browsers and another way to other browsers.
最好的办法就是让你的页面足够智能,可以根据不同的浏览器来做出不同的反应

JavaScript includes an object called the Navigator object, that can be used for this purpose.
JS包含导航对象,它可以实现这个目的

The Navigator object contains information about the visitor's browser name, browser version, and more.
导航对象包含有关访问者浏览器的名称,版本以及....


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("<br />")
document.write("Browser version: "+ version)
</script>
</body>
</html>

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 starts the appVersion string with the number 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>

评论 (0) All

登陆 | 还没注册?