- 浏览: 1045320 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
nieanan:
感谢,很有帮助。
eclipse 改变JAVA_HOME路径 -
Orange_killer:
写的什么东西,文不对题
Hibernate Search大数据量手动建立索引 -
xiaoasha:
org.eclipse.equinox.servlet.api ...
《OSGI实战》遇到的问题 -
powertech:
写的挺细,有用!
SyntaxError: Non-ASCII character Python、Unicode和中文 -
huang_yong:
public class XMLUtil {
pri ...
XStream 去除生成的XML节点的class="list"
The navigator object
The navigator object was conceived back in the days when Netscape Navigator reined supreme. These days it serves as much as an irony of NS's diminished market share as way of probing browser information.
The navigator object of JavaScript contains the following core properties:
appCodeName | The code name of the browser. |
appName | The name of the browser (ie: Microsoft
Internet Explorer
). |
appVersion | Version information for the browser
(ie: 4.75 [en] (Win98; U)
). |
cookieEnabled | Boolean that indicates whether the browser has cookies enabled. |
language | Returns the default language of the
browser version (ie: en-US
). NS and Firefox only.
|
mimeTypes[] | An array of all MIME types supported by the client. NS and Firefox only. |
platform[] | The platform of the client's computer
(ie: Win32
). |
plugins | An array of all plug-ins currently installed on the client. NS and Firefox only. |
systemLanguage | Returns the default language of the operating system
(ie: en-us
). IE only.
|
userAgent | String passed by browser as user-agent
header. (ie: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
) |
userLanguage | Returns the preferred language setting of the user
(ie: en-ca
). IE only.
|
Let's see exactly what these properties reveal of the browser you're currently using :
<!---->
<script type="text/javascript">
with (document){
write("<b>appCodeName:</b> "+navigator.appCodeName+"<br />")
write("<b>appName:</b> "+navigator.appName+"<br />")
write("<b>appVersion:</b> "+navigator.appVersion+"<br />")
write("<b>userAgent:</b> "+navigator.userAgent+"<br />")
write("<b>platform:</b> "+navigator.platform+"<br />")
}
</script>
appCodeName:
Mozilla
appName:
Netscape
appVersion:
5.0 (Windows; en-US)
userAgent:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
platform:
Win32
<!---->
At a glance
At a glance at the above table, you may be swayed towards turning to the following two properties to do your browser detection bidding:
navigator.appName
navigator.appVersion
After all, you are trying to detect a browser's name and
version right? However, they both will most likely mislead you. In
browsers such as various versions of Netscape and Firefox, these two
properties return simply "Netscape" for appName
, and 5.0
for appVersion
without any further distinction for Firefox
and its version, and hence are pretty much useless in the real world.
For example, in both Firefox 1.x and Firefox 2.x, these two properties
return:
appName: Netscape appVersion: 5.0 (Windows; en-US)
We need to turn to a property that's more thorough in its
investigative work if we want more consistency and accuracy, and that
turns out to be navigator.userAgent
.
Detecting Firefox x.x
In Firefox 2.0.0.13 for example,
the userAgent
property reads:
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
The detail we're interested in apparently lies at the very
end, or Firefox/2.0.0.13
. Different versions of Firefox will
contain a different version number, but the pattern is consistent enough.
The part we're interested in occurs after the string "Firefox/
",
or the exact version number. There are many ways to get to it using either
standard String
or RegExp
methods- I'm opting for the later here:
<script type="text/javascript">
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for
Firefox/x.x or Firefox x.x (ignoring remaining digits);
var ffversion=new Number(RegExp.$1) // capture x.x portion and store as
a number
if (ffversion>=3)
document.write("You're using FF 3.x or above")
else if (ffversion>=2)
document.write("You're using FF 2.x")
else if (ffversion>=1)
document.write("You're using FF 1.x")
}
else
document.write("n/a")
</script>
Output: <!----> <script type="text/javascript"> if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits); var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number if (ffversion>=3) document.write("You're using FF 3.x or above") else if (ffversion>=2) document.write("You're using FF 2.x") else if (ffversion>=1) document.write("You're using FF 1.x") } else document.write("n/a") </script> You're using FF 3.x or above<!---->
Basically, I'm capturing just the versonMajor.versionMinor portion of the full version number of Firefox (ie: 2.0.0.13 becomes simply 2.0), and using that as basis to detect the various versions of Firefox. Delving any deeper, and the returned version may no longer be a number but a string (ie: 2.0.0), which makes numeric comparisons cumbersome.
Detecting IE x.x
In IE 7.0 for example,
the userAgent
property reads:
UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
So the part we're interested in lies in the middle, or
MSIE 7.0;
. If you try a shortcut and use
parseFloat
on the entire string to get to the 7.0
portion, it won't
work. This
is due to the way parseFloat
works- by returning the first number it
encounters, which in this case is 4.0. Once again we need to use either
standard
String
or
RegExp
methods again to get to the actual version number; below I'm
using RegExp as well:
<script type="text/javascript"> if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x; var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number if (ieversion>=8) document.write("You're using IE8 or above") else if (ieversion>=7) document.write("You're using IE7.x") else if (ieversion>=6) document.write("You're using IE6.x") else if (ieversion>=5) document.write("You're using IE5.x") } else document.write("n/a") </script>
Output: <!----> <script type="text/javascript"> if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x; var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number if (ieversion>=8) document.write("You're using IE8 or above") else if (ieversion>=7) document.write("You're using IE7.x") else if (ieversion>=6) document.write("You're using IE6.x") else if (ieversion>=5) document.write("You're using IE5.x") } else document.write("n/a") </script> n/a<!---->
Detecting Opera x.x
Detecting Opera using the navigator
object at
first appears to be tricky business due to the browser's identity
crisis. You see, Opera 8 and below by default identifies itself as IE6
(or lower) in the navigator
object. Users can override this
setting under "Edit Site Settings
" in the toolbar to identify as
Opera or even another browser instead. Starting in Opera 9, the browser
regains its confidence and identifies by default as itself, Opera,
though users can still modify this setting manually in the toolbar. The
bottom line is, Opera can appear as either Opera, Internet Explorer, or
another browser within a designated list in the navigator
object.
Lets take a
look at what navigator.userAgent
in Opera 8.5 returns
depending on what it has chosen to identify itself as (whether
automatically or manually):
As IE6:
Mozilla/4.0 (compatible; MSIE 6.0; Windows XP) Opera 8.5
[en]
As Moz5:
Mozilla/5.0 (Windows XP; U) Opera 8.5 [en]
As Opera:
Opera/8.5 (Windows XP; U) [en]
Notice how if it's set to identify as IE, MSIE 6.0
appears
within the string, while if set to identify as Mozilla,
Mozilla/5.0
appears instead. As Opera itself, Opera/8.5
appears. In all three cases, the one commonality that we can exploit to
actually detect Opera and its true version regardless of which identify
it's decided to take on is the string "Opera x.x
" or "Opera/x.x
"
within navigator.userAgent
. In other words, there are two
versions of the target string we need to be aware of. With that said,
here's how you might go about testing for a specific version of Opera,
which turns out to be no different than the technique used for
detecting, say, Firefox:
<script
type="text/javascript">
//Note: userAgent in Opera9.24 WinXP returns: Opera/9.24 (Windows NT 5.1; U;
en)
// userAgent in Opera
8.5 (identified as IE) returns: Mozilla/4.0 (compatible; MSIE 6.0; Windows
NT 5.1) Opera 8.50 [en]
// userAgent in Opera
8.5 (identified as Opera) returns: Opera/8.50 (Windows NT 5.1; U) [en]
if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Opera/x.x
or Opera x.x (ignoring remaining decimal places);
var oprversion=new Number(RegExp.$1) // capture x.x portion and store as a
number
if (oprversion>=10)
document.write("You're using Opera 10.x or above")
else if (oprversion>=9)
document.write("You're using Opera 9.x")
else if (oprversion>=8)
document.write("You're using Opera 8.x")
else if (oprversion>=7)
document.write("You're using Opera 7.x")
else
document.write("n/a")
}
else
document.write("n/a")
</script>
Output: <!----> <script type="text/javascript"> //Note: userAgent in Opera9.24 WinXP returns: Opera/9.24 (Windows NT 5.1; U; en) // userAgent in Opera 8.5 (identified as IE) returns: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Opera 8.50 [en] // userAgent in Opera 8.5 (identified as Opera) returns: Opera/8.50 (Windows NT 5.1; U) [en] if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Opera/x.x or Opera x.x (ignoring remaining decimal places); var oprversion=new Number(RegExp.$1) // capture x.x portion and store as a number if (oprversion>=10) document.write("You're using Opera 10.x or above") else if (oprversion>=9) document.write("You're using Opera 9.x") else if (oprversion>=8) document.write("You're using Opera 8.x") else if (oprversion>=7) document.write("You're using Opera 7.x") else document.write("n/a") } else document.write("n/a") </script> n/a
发表评论
-
Window.ShowModalDialog的参数问题(父窗体向子窗体传值)
2010-03-22 12:57 2871基本介绍: showModalDialog() ... -
Textarea在光标停留处插入文字
2010-03-17 15:51 1812<!-- function Insert(str ... -
dojo.io.iframe.send 获得返回数据
2010-02-23 20:28 5360使用dojo.io.iframe.send提交表单时发现老是取 ... -
请问用javascript如何实现求两个日期相隔的天数
2010-01-21 17:32 1948方法一: /******************* ... -
JavaScript利用ActiveX导出Excel,Word
2010-01-15 16:03 1665<HTML> <HEAD> ... -
JavaScript检查ActiveX控件是否已经安装过
2010-01-14 16:52 1904function detectPlugin(CLSID,fu ... -
通过frames.frameElement获得当前页面iframe对象
2009-10-21 09:33 2650如果有个页面嵌在一个iframe里面,现在在这个页面里面如何获 ... -
textarea中的OnChange事件与onkeyup事件
2009-09-24 14:25 11668有个textarea,想实现字符改变,显示的字符数也随着变化 ... -
javascript检测document关闭的好方法
2009-09-23 15:31 1530如果使用window.unload或者window.befor ... -
string 扩展endWith
2009-04-03 17:45 1561string 扩展endWith <SCRIPT ... -
用JS过滤html标签的代码
2009-03-26 21:40 8533如果只是把< >类似的标记统统去掉,并不需要考虑别 ... -
Iframe实现的ajax如何实现POST请求?
2008-11-21 17:21 9779曾经看到有人用iframe来实现AJAX,我想了一下 ... -
Element nodeType values
2008-10-31 16:49 1418Last updated: Februrary 27th, 2 ... -
JavaScript获取HTML DOM节点元素的方法的总结
2008-10-31 16:43 2847在Web应用程序特别是Web2.0程序开发中,经常要获取页面中 ... -
firefox 2 marquee兼容
2008-10-31 16:40 4630新作的项目里面有个页面用到了<marquee>标签 ... -
不用js多浏览器兼容纯DIV/CSS对联漂浮广告代码
2008-10-20 10:04 2877CSS代码: .r1{width:80px;height:80 ... -
删除已经加载的css stylesheet
2008-10-17 16:14 1209想要删除已经加载的css stylesheet (<li ... -
mac下面的safari不支持element.attribute['attrName'].value
2008-10-15 17:48 1599今天又发现mac下面的一个问题: mac下面的safari ... -
xml作为参数的兄弟们要注意了
2008-10-13 10:45 1869将xml作为参数的兄弟们注意了,请将你们的xml,encode ... -
safari中javascript的跨域权限更加严格
2008-09-12 11:23 2338写过ajax的人都知道,如果当前页面是http://www.y ...
相关推荐
JS判断浏览器之Navigator对象 js怎样区分浏览器的实现代码
本文实例讲述了js判断浏览器版本以及浏览器内核的方法。分享给大家供大家参考。具体实现方法如下: js判断是否移动端及浏览器内核 var browser = { versions: function() { var u = navigator.userAgent; return...
JS判断浏览器之Navigator对象
今天用到JS判断浏览器类型,于是就系统整理了一下,便于后期使用。 /* * 描述:判断浏览器信息 * 编写:LittleQiang_w * 日期:2016.1.5 * 版本:V1.1 */ //判断当前浏览类型 function BrowserType() { ...
使用JS在浏览器中判断当前网络状态的几种方法如下: 1. navigator.onLine 2. ajax请求 3. 获取网络资源 4. bind() 1. navigator.onLine 通过navigator.onLine判断当前网络状态: if(navigator.onLine){ ... }...
今天突然看到一段很好的代码,分享给大家,顺便也给自己留个笔记,js判断浏览器的环境具体如下: 方法一: // 判断浏览器函数 function isMobile(){ if(window.navigator.userAgent.match(/(phone|pad|pod|...
function getBrowser() { var i = window.navigator.userAgent; var isChrome = i.indexOf("Chrome") && window.chrome; var issafari=i.indexOf("safari") && window....js判断不同浏览器内核调用不同css样式
本篇文章主要分享了通过window.navigator来判断浏览器及其版本信息的实例代码。具有一定的参考价值,下面跟着小编一起来看下吧
浏览器代码名称:navigator.appCodeName 浏览器名称:navigator.appName 浏览器版本号:navigator.appVersion 对Java的支持:navigator.javaEnabled() MIME类型(数组):navigator.mimeTypes 系统平台:navigator....
下面列举一下常用的判断方法 1、判断浏览器是否为IE document.all ? ‘IE’ : ‘others’:在IE下document.all值为1,而其他浏览器下的值为0; navigator.userAgent.indexOf(“MSIE”)>0 ? ‘IE’ : ‘others’:...
浏览器版本的判断,比较的简单,对新人很有帮助 var browser=navigator.appName var b_version=navigator.appVersion var version=parseFloat(b_version) [removed]("Browser name: "+ browser) [removed](...
因为360的流氓性质,现在已经在 navigator.userAgent 检测不到360浏览器的特征。只能使用其他办法。这里是我自己找的一种办法 。 缺点是对移动端浏览器设备没统计和测试过,有什么问题和建议可以在留言中补充,我...
这篇文章主要介绍了如何基于js判断浏览器版本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 浏览器类型判断 不考虑对 IE9 以下浏览器的判断 function ...
因为业务需求,项目经理让我做一个判断当前浏览器是否是谷歌浏览器,并确定谷歌浏览器版本,不满足谷歌浏览器和版本就显示一个页面提示升级,满足条件就跳转到我们的一个项目,下面先来实现这个: 1.当前浏览器是否...
判断是否为Safari浏览器 判断微信环境以及其他类型 判断IOS/安卓 const u = navigator.userAgent; const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); if (isiOS) { console.log('IOS') }else{ console....
工作中需要用到判断浏览器类型,网上找到的内容不怎么全,故在此进行一下总结。 一、不同浏览器及版本下User-Agent信息 待续…..欢迎补充 二、根据User-Agent信息进行判断 以下代码目前还判断不了win10下的edge //...
本文实例讲述了JS判断浏览器类型与操作系统的方法。分享给大家供大家参考,具体如下: navigator.userAgent : userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。 navigator.platform...
可以通过判断浏览器的userAgent,用正则来判断手机是否是ios和Android客户端。 var u = navigator.userAgent; isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端 isiOS = !!u....
既判断分辨率,也判断浏览器 重新完善代码,使之成为判断浏览器类型屏幕分辨率自动调用不同CSS的代码。 代码如下:[removed] <!– if (window.navigator.userAgent.indexOf(“MSIE”)>=1) { var IE1024=””; ...