Browser judgment based on Features

Source: Internet
Author: User
Today, I saw a browser that I used a long time ago on csdn, but most of them failed. Now I have modified it. I have tested several browsers on my local machine.

VaR sys = {},
UA = navigator. useragent;
If (window. activexobject)
SYS. Ie = UA. Match (/MSIE ([\ D.] +)/) [1]
Else if (window. Google & window. Chrome ){
SYS. Chrome = UA. Match (/chrome \/([\ D.] +)/) [1]}
Else if (window. components)
SYS. Firefox = UA. Match (/Firefox \/([\ D.] +)/) [1]
Else if (window. Opera)
SYS. Opera = UA. Match (/opera. ([\ D.] +)/) [1]
Else if (! Navigator. taintenabled)
SYS. Safari = UA. Match (/version \/([\ D.] +)/) [1];

// Perform the following tests:
If (SYS. ie) document. Write ('ie: '+ SYS. ie );
If (SYS. Firefox) document. Write ('Firefox: '+ SYS. Firefox );
If (SYS. Chrome) document. Write ('chrome: '+ SYS. Chrome );
If (SYS. Opera) document. Write ('Opera: '+ SYS. Opera );
If (SYS. Safari) document. Write ('safari: '+ SYS. Safari );

 

We sorted out one more Feature Detection:

VaR browser = function () {// try to use features for Detection
VaR UA = navigator. useragent,
B = {
IE :!! Window. activexobject,
//! + '\ V1'
//!! Window. vbarray
IE6 :! "1" [0],
Ie67:/MSIE [67]/. Test (UA ),
Ie9 :!! Document.doc umentmode & document.doc umentmode = 9,
Chrome :!! (Window. Google & window. Chrome ),
Firefox :!! Window. components,
Safari :!! Navigator. Vendor &&! Top. Chrome ,//(! Navigator. taintenabled &&! Window. Chrome), the strange IE6/7 error,
Opera :!! Window. opera,
Version: (UA. Match (/\ s + (? : MSIE | chrome | Firefox | Version) [\/] ([\ D.] +)/) | [0, 0]) [1]
};
Return B
}()
// Display the result
Document. Write ("<br/>" + navigator. useragent + "<br/> ")
VaR c = browser;
For (var n in c ){
Document. Write (n + ":" + C [N] + "<br/> ")
}

 

 

UA Detection:

VaR BB = function (){
VaR UA = navigator. useragent,
Check = function (r) {return R. Test (UA )},
B = {
IE: Check (/MSIE /),
IE6: Check (/MSIE 6 /),
Ie67: Check (/MSIE [67]/),
Ie9: Check (/MSIE 9 /),
Chrome: Check (/Chrome /),
Firefox: Check (/Firefox /),
Safari: Check (/Safari /)&&! Top. Chrome,
Opera: Check (/Opera /),
Version: (UA. Match (/\ s + (? : MSIE | chrome | Firefox | Version) [\/] ([\ D.] +)/) | [0, 0]) [1]
};
Return B
}()

 

 

Related links:

Javascript browser judgment

Browser detect

 

 

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.