Js recognition of different browsers and js Recognition

Source: Internet
Author: User

Js recognition of different browsers and js Recognition

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 = {
// Browser
Ie: 0,
Firefox: 0,
Safari: 0,
Konq: 0,
Opera: 0,
Chrome: 0,
Ver: null
};
Var ua = navigator. userAgent;
// Browser detection is ordered
If (window. opera) {// opera disguise, so detection is preferred
Engine. 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 Chrome or Safari is used
If (/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 {
// Determine the version number in an approximate way
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 Firefox
If (/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 the code for Chrome
} Else if (client. browser. safari ){
// Execute the code for Safari
}
} Else if (client. engine. gecko ){
If (client. browser. firefox ){
// Execute the code for Firefox
} Else {
// Execute the code for other Gecko browsers
}
}


Js detects the browser type (please write code)

Will only distinguish Firefox and IE, will not be the window of the world ....

Such
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script>
If (document. all) {alert ("ie ");}
Else alert ("s B Firefox ");
</Script>
</Head>

<Body>
</Body>
</Html>

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>

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.