PDA

View Full Version : JS browser detect & page redirect


yauhui
11 Jan 2009, 08:42
Well I'm trying to make a Javascript that detects the user's browser, and if the user uses a certain browser, redirects the user to a different page.

I got the Javascript from several sources and put them together, and they don't work :(

Here's the Javascript for anyone willing to help.

var BrowserDetect = {
init: function () {
this.browser = this.searchString(this.dataBrowser) || "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.version SearchString.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.platform,
subString: "Linux",
identity: "Linux"
}
]

};
BrowserDetect.init();

if (BrowserDetect.browser: "Safari") function move() { window.location = 'index2.html' } else

if (BrowserDetect.browser: "Firefox") function move() { window.location = 'index2.html' } else

Thanks in advance

thomasp
11 Jan 2009, 11:05
Assuming javascript "if" statements work in the same way as Matlab "if" statements, the final line ain't gonna work, since Firefox will already have been redirected to somewhere other than "index2.html"

However, I know pretty much nothing about javascript, although I'd have thought an "if" statement is pretty much the same in any language.

farazparsa
12 Jan 2009, 04:07
Team 17 - Tech Support.

yauhui
12 Jan 2009, 08:56
Team 17 - Tech Support.

I don't see how Team17 Tech Support has anything to do with Javascript.

thomasp
12 Jan 2009, 09:01
Have you tried checking the source code of the various pages on the T17 website, specifically the tech support section?

yauhui
12 Jan 2009, 10:51
I don't see how it has ANYTHING to do with browser detect. Or maybe I'm at the wrong Tech Support page o_o

thomasp
12 Jan 2009, 13:22
The "submit ticket" page?

yauhui
13 Jan 2009, 09:31
The JS in Submit Ticket is to detect OS... o_o