Browser detection is often used in the work, if it is simple to judge what the current browser can be done by window.navigator.useragent such a JS to directly judge you can!
But for the browser version requirements are relatively high, if only a simple judgment of the name of the browser can not solve the problem, it will be done by other means! The existing jquery has abandoned the use of $.browser, so here I introduce a program called "Browser sniffer" to achieve!
The code is as follows: (Browserdetect.js)
varBrowserdetect ={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(vari=0;i<data.length;i++) { varDatastring =data[i].string; varDataprop =Data[i].prop; This. versionsearchstring = Data[i].versionsearch | |data[i].identity; if(datastring) {if(Datastring.indexof (data[i].substring)! =-1) returndata[i].identity; } Else if(Dataprop)returndata[i].identity; }}, Searchversion:function(datastring) {varindex = Datastring.indexof ( This. versionsearchstring); if(Index = =-1)return; returnParsefloat (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"}, {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:"Internet Explorer", Versionsearch:"MSIE"}, {string:navigator.userAgent, subString:"Gecko", Identity:"Mozilla", Versionsearch:"RV" }, { //For older Netscapesstring: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 ();
Use the following methods:
Browserdetect.browser; // the name of the browser, such as Firefox,ie Browserdetect.version; // version of the browser, for example, 7, one Browserdetect.os; // the operating system that the browser hosts, such as Windows, Linux
This JS compatibility is very good! Easy to use, and powerful!
Browser detection (browserdetect.js) using