Home > Web Front-end > JS Tutorial > body text

Under jQuery, the browser is determined through $.browser.

巴扎黑
Release: 2017-07-08 10:12:34
Original
1593 people have browsed it

Use jQuery to determine the type of browser, mainly using the $.browser tool class

Usage method:
$.browser.['browser keyword' ]

The code is as follows:

$(function() { 
if($.browser.msie) { 
alert("this is msie"); 
} 
else if($.browser.safari) 
{ 
alert("this is safari!"); 
} 
else if($.browser.mozilla) 
{ 
alert("this is mozilla!"); 
} 
else if($.browser.opera) { 
alert("this is opera"); 
} 
else { 
alert("i don't konw!"); 
}
Copy after login


Let’s take a look at the source code of jQuery:

The code is as follows:

var userAgent = navigator.userAgent.toLowerCase(); 
// Figure out what browser is being used 
jQuery.browser = { 
version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1], 
safari: /webkit/.test( userAgent ), 
opera: /opera/.test( userAgent ), 
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ), 
mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ) 
};
Copy after login

jQuery What is used is to match the userAgent with regular rules to determine the type and version of the browser.
If we want to determine whether the current browser is IE6, how should we determine it?
$.browser.msie&&($.browser.version == "6.0")&&!$.support.style
Similarly jQuery determines whether the browser is IE7
$.browser.msie&&($.browser .version == "7.0")
If you do not consider backward compatibility and do not want to import jQuery in order to determine each browser type
The easiest way to determine IE is

code As follows:

if(
document
.all){ 
alert("IE6") 
}
Copy after login

$.browser uses regular expression to match userAgent to determine the browser version and type. jquery.browser and jquery have been declared in the jquery1.3.2 version of the document. It is recommended that browser.version be deprecated and jquery.support can be used instead
But judging from the current situation, jquery.support is not easy to use and is very difficult to use. We still use $.browser honestly. Determine the browser type
If it is to determine the version of IE, I still recommend using IE’s conditional expression to write JS

The code is as follows:

<!--[if IE]> 
<script type="text/
javascript
"> 
alert("ie") 
</script> 
<![endif]--> 
<!--[if IE 6]> 
<script type="text/javascript"> 
alert("ie6") 
</script> 
<![endif]--> 
<!--[if IE 7]> 
<script type="text/javascript"> 
alert("ie7") 
</script> 
<![endif]-->
Copy after login

This is easier than our manual It is more accurate to judge the IE version through $.browser, and there is no need to remember how to use jquery's browser

The above is the detailed content of Under jQuery, the browser is determined through $.browser.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!