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: