A common but overestimated Javascript function is browser detection. Sometimes, you may want to give a description or load a page to prompt users to avoid using browsers such as Safari.
Usage:
Browser name: BrowserDetect. browser
Browser version: BrowserDetect. version
OS name: BrowserDetect. OS
Var BrowserDetect = {
Init: function (){
This. browser = this. searchString (this. databrown) | "An unknown browser ";
This. version = this. searchVersion (navigator. userAgent)
| This. searchVersion (navigator. appVersion)
| "An unknown version ";
This. OS = this. searchString (this. dataOS) | "an unknown OS ";
},
SearchString: function (data ){
For (var I = 0; I <data. length; I ++ ){
Var dataString = data [I]. string;
Var dataProp = data [I]. prop;
This. versionSearchString = data [I]. versionSearch | data [I]. identity;
If (dataString ){
If (dataString. indexOf (data [I]. subString )! =-1)
Return data [I]. identity;
}
Else if (dataProp)
Return data [I]. identity;
}
},
SearchVersion: function (dataString ){
Var index = dataString. indexOf (this. versionSearchString );
If (index =-1) return;
Return parseFloat (dataString. substring (index + this. versionSearchString. length + 1 ));
},
DataBrowser :[
{
String: navigator. userAgent,
SubString: "Chrome ",
Identity: "Chrome"
},
{String: navigator. userAgent,
SubString: "OmniWeb ",
VersionSearch: "OmniWeb /",
Identity: "OmniWeb"
},
{
String: navigator. vendor,
SubString: "Apple ",
Identity: "Safari ",
VersionSearch: "Version"
},
{
Prop: window. opera,
Identity: "Opera"
},
{
String: navigator. vendor,
SubString: "iCab ",
Identity: "iCab"
},
{
String: navigator. vendor,
SubString: "KDE ",
Identity: "Konqueror"
},
{
String: navigator. userAgent,
SubString: "Firefox ",
Identity: "Firefox"
},
{
String: navigator. vendor,
SubString: "Camino ",
Identity: "Camino"
},
{// For newer Netscapes (6 +)
String: navigator. userAgent,
SubString: "Netscape ",
Identity: "Netscape"
},
{
String: navigator. userAgent,
SubString: "MSIE ",
Identity: "Explorer ",
VersionSearch: "MSIE"
},
{
String: navigator. userAgent,
SubString: "Gecko ",
Identity: "Mozilla ",
VersionSearch: "rv"
},
{// For older Netscapes (4 -)
String: navigator. userAgent,
SubString: "Mozilla ",
Identity: "Netscape ",
VersionSearch: "Mozilla"
}
],
DataOS :[
{
String: navigator. platform,
SubString: "Win ",
Identity: "Windows"
},
{
String: navigator. platform,
SubString: "Mac ",
Identity: "Mac"
},
{
String: navigator. userAgent,
SubString: "iPhone ",
Identity: "iPhone/iPod"
},
{
String: navigator. platform,
SubString: "Linux ",
Identity: "Linux"
}
]
};
BrowserDetect. init ();