Home >Web Front-end >JS Tutorial >How to determine the browser version type based on jQuery1.9 version_jquery
In versions before jquery.1.9, you can use $.browser to easily determine the browser type and version. However, in 1.9 and later versions, $.browser has been deleted. Here is an introduction to how to achieve this. I hope this feature can help friends in need.
1. Custom code:
$.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());
In the above code, the expression return value after the equal sign is of Boolean type and is used to indicate whether this browser is supported. This achieves a custom $.browser effect.
2. Determine the IE6 browser:
Use the following code before jquery1.9:
if ($.browser.msie && 7 > $.browser.version) {}
jquery1.9 and later use the following code:
if ('undefined' == typeof(document.body.style.maxHeight)) {}
3. Determine IE6-IE8 browsers:
if (!$.support.leadingWhitespace) {}
To sum up, our requirements have been basically achieved, so I won’t introduce it much here.
Expand knowledge points:
Definition and usage of jQuery.browser:
Browser kernel identification, judged based on navigator.userAgent.
Available values: safari, opera, msie and mozilla.
Browser object detection technology used with this attribute provides reliable browser detection support.
Removed in jQuery 1.9.
If the web page is running in the opera browser, then jQuery.browser.opera will return true, otherwise it will return false.
Other attribute values can be deduced in the same way.
Example code:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <script type="text/javascript" src="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>
Usage of typeof operator:
The typeof operator is placed before the operand to detect the data type of the operand and returns a string to describe the type of the operand.
Operands can be variables or values.
Possible values returned by typeof operator:
1. If the variable is not assigned a value or the variable value is assigned undefined, then undefined is returned.
Instance:
var a console.log(typeof(a))
Variable a has not been assigned a value. At this time, the variable is implicitly assigned undefined by default. Output result: undefined.
var a=undefined; console.log(typeof(a))
Variable a is assigned the value undefined. Output result: undefined.
2. If the variable or value is of Boolean type, return boolean.
Example code:
console.log(typeof (true))
Output result: boolean.
var a=2,b=1,c=3,d; d=a+b; console.log(typeof(c==d))
Output result: boolean.
3. If the variable or value is a numeric type, return number.
console.log(typeof(1))
Output result: number.
4. If the variable or value is a string, return string.
console.log(typeof("mayi"))
Output result: string.
console.log(typeof("a"))
There are no character types in ECMAScript. So this code will also output string.
5. If the variable is a reference type or null, object will be returned.
Note: null can be considered as a placeholder for an object, so the return value is also object.
Example code:
var a=new Date; console.log(typeof(a))
Create a time object instance a, which is a reference type. Output result: objct.
console.log(typeof(null))
Output result: object.
6. If the variable is a function, return function
console.log(typeof(function(){alert("大家好")}))
Output result: function.
var a=function(){alert("大家好")} console.log(typeof(a))
Output result: function.