导航首页 » 技术教程 » 基于jQuery1.9版本如何判断浏览器版本类型
基于jQuery1.9版本如何判断浏览器版本类型 73 2024-03-14   

在jquery.1.9以前的版本,可以使用$.browser很轻松的判断浏览器的类型和版本,但是在1.9中和以后的版本中,$.browser已经被删除,下面就介绍一下如何实现此功能,希望能够给需要的朋友带来帮助。

一.自定义代码:

$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase()); 
$.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase()); 
$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase()); 
$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase()); 

以上代码中,等号后面的表达式返回值是布尔类型的,用来表示是否支持此浏览器。这样就实现了自定义的$.browser效果。

二.判断IE6浏览器:

jquery1.9之前使用以下代码:

if ($.browser.msie && 7 > $.browser.version) {} 

jquery1.9和之后使用以下代码:

if ('undefined' == typeof(document.body.style.maxHeight)) {} 

三.判断IE6-IE8浏览器:

if (!$.support.leadingWhitespace) {} 

综上所述,基本实现了我们的要求,这里就不多介绍了。

扩展知识点:

jQuery.browser的定义和用法:

浏览器内核标识,依据 navigator.userAgent 判断。

可用值: safari 、opera 、msie 和mozilla。

浏览器对象检测技术与此属性共同使用可提供可靠的浏览器检测支持。

在jQuery 1.9中已经删除。

如果网页在opera浏览器中运行,那么jQuery.browser.opera会返回true,否则返回false。
其他的属性值以此类推。

实例代码:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.gimoo.net/" />
<title>绿夏网</title>
<script type="text/javascript" src="http://www.gimoo.net/t/1904/mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
alert($.browser.msie); 
}); 
</script> 
</head> 
<body> 
如果在IE浏览器中运行则返回true,否则返回false。 
</body> 
</html>

typeof 运算符的用法:

typeof运算符放在操作数之前,可以检测此操作数的数据类型,并返回一个字符串用以说明操作数的类型。
操作数可以是变量或者值等。

typeof运算符可能返回的值:

一.如果变量未赋值或者变量值赋值为undefined,则返回undefined。

实例实例:

var a
console.log(typeof(a))

变量a没有被赋值,这个时候变量被默认隐式赋值undefined。输出结果:undefined。

var a=undefined;
console.log(typeof(a))

变量a被赋值undefined。输出结果:undefined。

二.如果变量或者值是布尔类型,则返回boolean。

实例代码:

console.log(typeof (true))

输出结果:boolean。

var a=2,b=1,c=3,d;
d=a+b;
console.log(typeof(c==d))

输出结果:boolean。

三.如果变量或者值是数值类型,则返回number。

console.log(typeof(1))

输出结果:number。

四.如果变量或者值是字符串,则返回string。

console.log(typeof("mayi"))

输出结果:string。

console.log(typeof("a"))

ECMAScript中没有字符类型。所以此段代码也会输出string。

五.变量是引用类型或者null,则会返回object。

注:null可以认为是对象的占位符,所以返回值也是object。

实例代码:

var a=new Date;
console.log(typeof(a))

建立一个时间对象实例a,它是一个引用类型。输出结果: objct。

console.log(typeof(null))

输出结果: object。

六.如果变量是函数,则返回function

console.log(typeof(function(){alert("大家好")}))

输出结果:function。

var a=function(){alert("大家好")}
console.log(typeof(a))

输出结果:function。



!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103