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

Use $.browser to determine the browser under jQuery._jquery

WBOY
Release: 2016-05-16 18:08:26
Original
909 people have browsed it

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

Copy code 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!");
}

Let’s take a look at the source code of jQuery:
Copy code 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 )
};

jQuery uses regular rules to match userAgent 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
Same as jQuery. 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 judge IE is
Copy the code The code is as follows:

if(document.all ){
alert("IE6")
}

$.browser is a .jquery1.3.2 version of the document that uses regular expressions to match userAgent to determine the browser version and type. It has been stated that jquery.browser and jquery.browser.version are recommended to be deprecated and jquery.support can be used instead
However, judging from the current situation, jquery.support is not easy to use and is very difficult to use. We still Honestly use $.browser to determine the browser type
If it is to determine the version of IE, I still recommend using IE’s conditional expressions to write JS
Copy code The code is as follows:





This is more accurate than manually judging the IE version through $.browser, and there is no need to remember how to use jquery's browser.
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!