JS判断浏览器之Navigator对象_Javascript教程-查字典教程网
JS判断浏览器之Navigator对象
JS判断浏览器之Navigator对象
发布时间:2016-12-30 来源:查字典编辑
摘要:TheNavigatorObject导航对象TheJavaScriptNavigatorobjectcontainsallinformati...

TheNavigatorObject

导航对象

TheJavaScriptNavigatorobjectcontainsallinformationaboutthevisitor'sbrowser.WearegoingtolookattwopropertiesoftheNavigatorobject:

JS导航对象包含所有有关访问这者浏览器的信息。我们将看看两个导航对象的产物:

appName-holdsthenameofthebrowser

appName-含浏览器的名称

appVersion-holds,amongotherthings,theversionofthebrowser

appVersion-浏览器版本

Example

举例

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

Thevariablebrowserintheexampleaboveholdsthenameofthebrowser,i.e."Netscape"or"MicrosoftInternetExplorer".

上面例子中变量brower(浏览器)被赋加了浏览器的名称,网景或是IE(或其他)

TheappVersionpropertyintheexampleabovereturnsastringthatcontainsmuchmoreinformationthanjusttheversionnumber,butfornowweareonlyinterestedintheversionnumber.TopulltheversionnumberoutofthestringweareusingafunctioncalledparseFloat(),whichpullsthefirstthingthatlookslikeadecimalnumberoutofastringandreturnsit.

上面例子中的appVersion属性返回一串包含比版本号更多的信息字符,但现在我们只要版本号。要从字符串中提出版本号我们使用一个叫parseFloat()的函数来返回数字。

IMPORTANT!TheversionnumberisWRONGinIE5.0orlater!MicrosoftstarttheappVersionstringwiththenumbers4.0.inIE5.0andIE6.0!!!Whydidtheydothat???However,JavaScriptisthesameinIE6,IE5andIE4,soformostscriptsitisok.

(有关JS返回IE版本号的问题)

Example

举例

Thescriptbelowdisplaysadifferentalert,dependingonthevisitor'sbrowser:

根据访问者的浏览器,(不同的浏览器)下面的脚本将显示不同的警示:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类