How do I determine the browser version type based on es0EN 1.9

  • 2020-11-30 08:09:46
  • OfStack

In previous versions of jquery.1.9, it was easy to use $.browser to determine the type and version of the browser, but in 1.9 and later versions, $.browser has been removed. Here's how to do it.

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 Boolean to indicate whether the browser is supported or not. This implements the custom $.browser effect.

2. IE6 browser:

Use the following code before jquery1.9:


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

jquery1.9 and after use the following code:


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

3. IE6-IE8 Browser:


if (!$.support.leadingWhitespace) {} 

To sum up, the basic realization of our requirements, here is not to introduce more.

1.

Definition and usage of browser:

Browser kernel identity, as determined by ES41en.userAgent.

Available values: safari, opera, msie and mozilla.

Browser object detection techniques used in conjunction with this property provide reliable browser detection support.

This has been removed from jQuery 1.9.

If the page is running in opera browser, jQuery.browser.opera returns true, otherwise false.
And so on for the other attribute values.

Example code:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.ofstack.com/" />
<title> The home of the script </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> 
 If the IE Run in the browser and return true Otherwise return false .  
</body> 
</html>

Use of the typeof operator:

The typeof operator precedes the operand, detects the data type of the operand and returns a string indicating the type of the operand.
Operands can be variables, values, etc.

Values that the typeof operator might return:

1. Returns undefined if the variable is not assigned a value or undefined is assigned a value.

Examples:


var a
console.log(typeof(a))

The variable a is not assigned, at which point it is implicitly assigned to undefined by default. Output :undefined.


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

The variable a is assigned to undefined. Output :undefined.

2. Returns boolean if the variable or value is of a Boolean type.

Example code:


console.log(typeof (true))

Output :boolean.


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

Output :boolean.

3. Returns number if the variable or value is of numeric type.


console.log(typeof(1))

Output :number.

4. Returns string if the variable or value is a string.


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

Output :string.


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

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 is returned.

Note :null can be considered a placeholder for the object, so the return value is also object.

Example code:


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

Create a time object instance a, which is a reference type. Output: objct.


console.log(typeof(null))

Output: object.

6. Returns function if the variable is a function


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

Output :function.


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

Output :function.


Related articles: