jquery determines the browser type and version number

Source: Internet
Author: User

Use jquery to determine how your browser needs to use syntax: $.browser. [' Browser keyword ']

To determine the syntax examples for each browser:

The code is as follows Copy Code

$ (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!");
}

Determine browser type and version number

  code is as follows copy code

<script type= "Text/javascript" >
$ (document). Ready (function () {
var Bro=$.browser;
var binfo= "";
if (Bro.msie) {binfo= Microsoft Internet Explorer +bro.version}
if (Bro.mozilla) {binfo= "Mozilla Firefox" +bro.version}
if (Bro.safari) {binfo= "Apple Safari" +bro.version}
if (Bro.opera) {binfo= "opera" +bro.version}
alert (binfo);
$ ("#browser"). HTML (binfo);
})
</script>

<div id= "Browser" class= "info" >
Browser Info
</div>

Note: JQuery 1.9 Removes $.browser, if you use jquery1.9 to determine the browser type, we can use the following method to replace the method


Instead, it's $.support. In the newer version 2.0, IE 6/7/8 will no longer be supported. Later, if the user needs to support IE 6/7/8, only jQuery 1.9 can be used. If you want to fully support IE and mix jQuery 1.9 and 2.0, the official solution is:

The code is as follows Copy Code

<!--[If Lt IE 9]>
<script src= ' jquery-1.9.0.js ' ></script>
<! [endif]-->
<!--[if GTE IE 9]>
<script src= ' jquery-2.0.0.js ' ></script>
<! [endif]-->

In the long run, this helps to deal with the browser's characteristics separately in complex situations, rather than simply detecting browser types and versions. However, many of the old program porting may not be directly to the browser support features, so on the Internet to find a number of direct replacement solutions.

To determine the browser type:

The code is as follows Copy 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 ());

The expression following the equal sign returns the True/false, which can be used directly to replace the original $.browser.msie, and so on.

Check for IE6:

The code is as follows Copy Code

Old
if ($.browser.msie && 7 > $.browser.version) {}
New
if (' undefined ' = = typeof (Document.body.style.maxHeight)) {}

Check for IE 6-8:

The code is as follows Copy Code

if (!$.support.leadingwhitespace) {}

The ultimate approach is to use another class library instead

The code is as follows Copy Code

var browserdetect = {
Init:function () {
This.browser = this.searchstring (this.databrowser) | | "An unknown browser";
This.version = This.searchversion (navigator.useragent)
|| This.searchversion (navigator.appversion)
|| "An unknown version";
This. OS = this.searchstring (This.dataos) | | "An unknown OS";
},
Searchstring:function (data) {
for (Var i=0;i<data.length;i++) {
var datastring = data[i].string;
var dataprop = Data[i].prop;
this.versionsearchstring = Data[i].versionsearch | | data[i].identity;
if (datastring) {
if (Datastring.indexof (data[i].substring)!=-1)
return data[i].identity;
}
else if (Dataprop)
return data[i].identity;
}
},
Searchversion:function (datastring) {
var index = datastring.indexof (this.versionsearchstring);
if (index = = 1) return;
Return parsefloat (datastring.substring (index+this.versionsearchstring.length+1));
},
Databrowser: [
{
String:navigator.userAgent,
SubString: "Chrome",
Identity: "Chrome"
},
{string:navigator.userAgent,
SubString: "OmniWeb",
Versionsearch: "omniweb/",
Identity: "OmniWeb"
},
{
String:navigator.vendor,
SubString: "Apple",
Identity: "Safari",
Versionsearch: "Version"
},
{
Prop:window.opera,
Identity: "Opera",
Versionsearch: "Version"
},
{
String:navigator.vendor,
SubString: "Icab",
Identity: "Icab"
},
{
String:navigator.vendor,
SubString: "KDE",
Identity: "Konqueror"
},
{
String:navigator.userAgent,
SubString: "Firefox",
Identity: "Firefox"
},
{
String:navigator.vendor,
SubString: "Camino",
Identity: "Camino"
},
{//For newer netscapes (6+)
String:navigator.userAgent,
SubString: "Netscape",
Identity: "Netscape"
},
{
String:navigator.userAgent,
SubString: "MSIE",
Identity: "Explorer",
Versionsearch: "MSIE"
},
{
String:navigator.userAgent,
SubString: "Gecko",
Identity: "Mozilla",
Versionsearch: "rv"
},
{//For older netscapes (from)
String:navigator.userAgent,
SubString: "Mozilla",
Identity: "Netscape",
Versionsearch: "Mozilla"
}
],
Dataos: [
{
String:navigator.platform,
SubString: "Win",
Identity: "Windows"
},
{
String:navigator.platform,
SubString: "Mac",
Identity: "Mac"
},
{
String:navigator.userAgent,
SubString: "IPhone",
Identity: "Iphone/ipod"
},
{
String:navigator.platform,
SubString: "Linux",
Identity: "Linux"
}
]

};
Browserdetect.init ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.