Js identifies different browsers Based on userAgent for judgment, jsuseragent
Check the browser, pay attention to the browser judgment order, mainly based on userAgent for judgment.
// Check the browser var client = function () {var engine = {ie: 0, gecko: 0, webkit: 0, khtml: 0, opera: 0, ver: null }; var browser = {// viewer ie: 0, firefox: 0, safari: 0, konq: 0, opera: 0, chrome: 0, ver: null}; var ua = navigator. userAgent; // the browser checks if (window. opera) {// opera disguise, so the engine is checked first. ver = browser. ver = window. opera. version (); engine. opera = browser. opera = parseFloat (engine. ver);} else if (/AppleWebKit \/(\ S + )/. test (ua) {engine. ver = RegExp ["$1"]; engine. webkit = parseFloat (engine. ver); // determine whether it is Chrome or Safariif (/Chrome \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. chrome = parseFloat (browser. ver);} else if (/Version \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. safari = parseFloat (browser. ver);} else {// approximately determines the version number var safariVersion = 1; if (engine. webkit <100) {safariVersion = 1;} else if (engine. webkit <312) {safariVersion = 1.2;} else if (engine. webkit <412) {safariVersion = 1.3;} else {safariVersion = 2;} browser. safari = browser. ver = safariVersion;} else if (/KHTML \/(\ S + )/. test (ua) |/Konqueror \/([^;] + )/. test (ua) {engine. ver = browser. ver = RegExp ["$1"]; engine. khtml = browser. konq = parseFloat (engine. ver);} else if (/rv :( [^ \)] +) \) Gecko \/\ d {8 }/. test (ua) {engine. ver = RegExp ["$1"]; engine. gecko = parseFloat (engine. ver); // determine if it is Firefoxif (/Firefox \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. firefox = parseFloat (browser. ver) ;}} else if (/MSIE ([^;] + )/. test (ua) {engine. ver = browser. ver = RegExp ["$1"]; engine. ie = browser. ie = parseFloat (engine. ver);} return {engine: engine, browser: browser };} (); // call if (client. engine. webkit) {// if it's WebKit if (client. browser. chrome) {// execute code for Chrome} else if (client. browser. safari) {// run the code for Safari} else if (client. engine. gecko) {if (client. browser. firefox) {// execute code for Firefox} else {// execute code for other Gecko browsers }}
How does js differentiate browsers and require code,
Alert (navigator. userAgent );
You can run this code. The values obtained by different browsers are different. The identification and version information of the browser are included.
If you are too lazy to write code, enter the following code in the address bar of your browser and press Enter:
Javascript: alert (navigator. userAgent)
It's really a failure:
Var isFF = navigator. userAgent. toLowerCase (). indexOf ('Firefox ');
Var isChrome = navigator. userAgent. toLowerCase (). indexOf ('chrome ');
Var isOpera = navigator. userAgent. toLowerCase (). indexOf ('Opera ');
Var isIE = navigator. userAgent. toLowerCase (). indexOf ('msie ');
How does js identify browsers?
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Script type = "text/javascript">
Function browserinfo (){
Var Browser_Name = navigator. appName;
Var Browser_Version = parseFloat (navigator. appVersion );
Var Browser_Agent = navigator. userAgent;
Var Actual_Version, Actual_Name;
Var is_IE = (Browser_Name = "Microsoft Internet Explorer"); // determines whether the browser is an Internet Explorer.
Var is_NN = (Browser_Name = "Netscape"); // determines whether the browser is a netscape Browser.
Var is_op = (Browser_Name = "Opera"); // determines whether the browser is operabrowser.
If (is_NN ){
// Upper 5.0 need to be process, lower 5.0 return directly
If (Browser_Version> = 5.0 ){
If (Browser_Agent.indexOf ("Netscape ")! =-1 ){
Var Split_Sign = Browser_Agent.lastIndexOf ("/");
Var Version = Browser_Agent.lastIndexOf ("");
Var Bname = Browser_Agent.substring (0, Split_Sign );
Var Split_sign2 = Bname. lastIndexOf ("");
Actual_Version = Browser_Agent.substring (Split_Sign + 1, Browser_Agent.length );
Actual_Name =... the remaining full text>