Qwrap browser Detection

Source: Internet
Author: User

Sometimes you need to do different things based on different browsers.
This requires us to check the browser and version.
Qwrap BrowserCodeAs follows.

 VaR  QW  =  {};
/* *
* @ Class browser JS running environment, browser and version information. (Browser only performs sniffing Based on useragent, which is not rigorous .)
* @ Singleton
* @ Namespace QW
*/
QW. Browser = Function (){
VaR Na = Window. Navigator,
UA = Na. useragent. tolowercase (),
Browsertester = / (MSIE | WebKit | gecko | presto | opera | safari | Firefox | chrome | Maxthon) [\/] ([\ D.] +) / Ig,
Browser = {Platform: Na. Platform };
UA. Replace (browsertester, Function (A, B, C ){
VaR Blower = B. tolowercase ();
Browser [blower] = C;
});
If (Browser. Opera ){ // Opera9.8 and later versions
UA. Replace ( / Opera. * version \/([\ D.] +) / , Function (A, B) {browser. Opera = B ;});
}
If (Browser. MSIE ){
Browser. ie = Browser. MSIE;
VaR V = Parseint (browser. MSIE );
Browser. IE6 = V = 6 ;
Browser. IE7 = V = 7 ;
Browser. IE8 = V = 8 ;
Browser. ie9 = V = 9 ;
}
Return Browser;
}();
If (QW. browser. ie ){ Try {Document.exe ccommand ( " Backgroundimagecache " , False , True );} Catch (E ){}}

Test code:

View code

<  Html  >  
< Body >
< Script >
VaR QW = {};
/* *
* @ Class browser JS running environment, browser and version information. (Browser only performs sniffing Based on useragent, which is not rigorous .)
* @ Singleton
* @ Namespace QW
*/
QW. Browser = Function (){
VaR Na = Window. Navigator,
UA = Na. useragent. tolowercase (),
Browsertester = / (MSIE | WebKit | gecko | presto | opera | safari | Firefox | chrome | Maxthon) [\/] ([\ D.] +) / Ig,
Browser = {Platform: Na. Platform };
UA. Replace (browsertester, Function (A, B, C ){
VaR Blower = B. tolowercase ();
Browser [blower] = C;
});
If (Browser. Opera ){ // Opera9.8 and later versions
UA. Replace ( / Opera. * version \/([\ D.] +) / , Function (A, B) {browser. Opera = B ;});
}
If (Browser. MSIE ){
Browser. ie = Browser. MSIE;
VaR V = Parseint (browser. MSIE );
Browser. IE6 = V = 6 ;
Browser. IE7 = V = 7 ;
Browser. IE8 = V = 8 ;
Browser. ie9 = V = 9 ;
}
Return Browser;
}();
If (QW. browser. ie ){ Try {Document.exe ccommand ( " Backgroundimagecache " , False , True );} Catch (E ){}}
</ Script >

< Script >
Function Print (o ){
VaR Html = [];
For ( VaR I In O) HTML. Push ( ' ( ' + ( Typeof O [I]) + ' ) ' + I + ' : ' + O [I]);
Document. Write (html. Join ( ' <Br/> ' ));
}
Print (QW. browser );
</ Script >
</ Body >
</ Html >

The detection result is as follows:

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.