We help IT Professionals succeed at work.

Java screening for undesirable browsers with redirect

sasnaktiv
sasnaktiv used Ask the Experts™
on
Does anybody have any idea why the first one of these html pages fails and the second one functions properly? It's beyond me!

THE FIRST PAGE BELOW FAILS WHEN TRYING TO LIST 2 SPECIFIC BROWSERS.
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
if (browserName=="Netscape")
{
window.location="welcome.html";
}
else
{
 if (browserName=="Microsoft Internet Explorer")
 {
  window.location="undesirableBrowser.html";
 }

else
{
 if (browserName=="Chrome")
 {
  window.location="undesirableBrowser.html";
 }

 else
  {
   window.location="welcome.html";
   }
}
//-->
</SCRIPT>
<meta name="robots" content="NOINDEX,FOLLOW">
</HEAD>
<BODY>
</BODY>
</HTML>


THIS SECOND ONE FUNCTIONS PROPERLY WHEN ONLY ONE 'ELSE IF' STATEMENT IS USED.

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
if (browserName=="Netscape")
{
window.location="welcome.html";
}
else
{
 if (browserName=="Microsoft Internet Explorer")
 {
  window.location="undesirableBrowser.html";
 }

 else
  {
   window.location="welcome.html";
   }
}
//-->
</SCRIPT>
<meta name="robots" content="NOINDEX,FOLLOW">
</HEAD>
<BODY>
</BODY>
</HTML>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
Perhaps use switch statement instead?
<script type="text/javascript">
switch(browserName){
	case'Netscape':
		window.location="welcome.html";
	break;
	case'Microsoft Internet Explorer':
		window.location="undesirableBrowser.html";
	break;
	case'Chrome':
		window.location="undesirableBrowser.html";
	break;
	default:
		window.location="welcome.html";
	break;
}
</script>

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
you're missing a closing brace


if (browserName=="Netscape")
{ 111
window.location="welcome.html";
} 111
else
{ 222                                                 <------ Does not get closed
 if (browserName=="Microsoft Internet Explorer")
 { 333
  window.location="undesirableBrowser.html";
 } 333

else
{ 444
 if (browserName=="Chrome")
 { 555
  window.location="undesirableBrowser.html";
 } 555

 else
  { 666
   window.location="welcome.html";
   } 666
} 444

Author

Commented:

Thanks for the replies, Guys.
No matter which solution I use, it always fails once I add a second 'else'
Any ideas?
Mick BarryJava Developer
Top Expert 2010

Commented:
did you add the missing brace?

how is it failing exactly?

Author

Commented:

Yes I added the brace, but maybe in the wrong place. Where should it go?
It fails by not moving off the html page. So the java is not having any effect.
Thanks
Mick BarryJava Developer
Top Expert 2010

Commented:
should go after the last closing brace

trying loading it in firefox with firebug enabled. that should give you details on why it is failing

Author

Commented:

Yes, I placed it in the last position. NG!
But oddly enough I just did it again and it functioned properly.
So I pushed it further by adding yet another 'else' statement and it failed partially.
What I mean is the first two else statement function as expected but the new third went to the worng page.
Oh, by the way, I've never heard of firebug.
<script type="text/javascript">

var browserName=navigator.appName; 
if (browserName=="Netscape")
{ 
window.location="WELCOME/Envelope.html";
}
else 
{ 
 if (browserName=="Microsoft Internet Explorer" )
 {
  window.location="browserFAILURE.html";
 }

else 
{ 
 if (browserName=="Opera" )
 {
  window.location="browserFAILURE.html";
 }

else 
{ 
 if (browserName=="Safari" )
 {
  window.location="browserFAILURE.html";
 }


 else
  {
   window.location="WELCOME/Envelope.html";
   }
}}}

</script>

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
Mick BarryJava Developer
Top Expert 2010

Commented:
using an else if would make things a lot simpler and more readable

http://www.tizag.com/javascriptT/javascriptelseif.php

Author

Commented:

Yes, I've already tried 'else if', yesterday as a matter of fact,
and I just did it again to check my sanity.
See the attached code.
Here's what happens.
Netscape goes to "WELCOME/Envelope.html" --- That's correct
IE goes to "browserFAILURE.html" --- That's correct
Opera goes to "browserFAILURE.html" --- That's correct
Chrome goes to "WELCOME/Envelope.html" -----But it's supposed to go to "browserFAILURE.h
Safari goes to "WELCOME/Envelope.html" -----That should to go to "browserFAILURE.html" too.

Go figure?
var browserName=navigator.appName; 
if (browserName=="Netscape")
{ 
window.location="WELCOME/Envelope.html";
}
else 
{ 
 if (browserName=="Microsoft Internet Explorer" )
 {
  window.location="browserFAILURE.html";
 }

else 
 if (browserName=="Opera" )
 {
  window.location="browserFAILURE.html";
 }


else 
 if (browserName=="Chrome" )
 {
  window.location="browserFAILURE.html";
 }

else 
 if (browserName=="Safari" )
 {
  window.location="browserFAILURE.html";
 }


 else
  {
   window.location="WELCOME/Envelope.html";
   }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
thats a different problem. appName isn't the most reliable as for example safari return "Netscape"
better to use userAgent

Author

Commented:

Thanks, but I get the same problems using: "var browserName=navigator.userAgent;"
I'm done for the day. I'll pick it up in the morning.
Good night!
Mick BarryJava Developer
Top Expert 2010

Commented:

Author

Commented:

Hi Savant,
Yes, I tried exactly that the other day, but the results were still problematic once it called on the second 'else' statement.
Greg AlexanderLead Developer

Commented:
Have you tried the switch statment? There is no else in the switch statement I provided.

Author

Commented:

What switch statement?
Greg AlexanderLead Developer
Commented:
This one
<script type="text/javascript">
switch(browserName){
	case'Netscape':
		window.location="welcome.html";
	break;
	case'Microsoft Internet Explorer':
		window.location="undesirableBrowser.html";
	break;
	case'Chrome':
		window.location="undesirableBrowser.html";
	break;
	default:
		window.location="welcome.html";
	break;
}
</script>

Open in new window

Author

Commented:

Oh yes I did. But that didn't work for me either. It doesn't go welcome.html nor undesireableBrowser.html  regardless of browser.

Author

Commented:

I also tried the following approach, but Camino goes to the wrong page. See code.

<script type="text/javascript">

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.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();





if (BrowserDetect.browser=="Netscape")
{ 
window.location="browserFAILURE.html";
}
else 
 
 if (BrowserDetect.browser=="Explorer" )
 {
  window.location="browserFAILURE.html";
 }
else 
 
 if (BrowserDetect.browser=="Flock" )
 {
  window.location="browserFAILURE.html";
 }

else 
 if (BrowserDetect.browser=="Opera" )
 {
  window.location="browserFAILURE.html";
 }


else 
 if (BrowserDetect.browser=="Chrome" )
 {
  window.location="browserFAILURE.html";
 }


else 
{ if (BrowserDetect.browser=="Camino" )
 {
  window.location="browserFAILURE.html";
 }

else 
 if (BrowserDetect.browser=="Safari" )
 {
  window.location="browserFAILURE.html";
 }


 else
  {
   window.location="WELCOME/Envelope.html";
   }


}

Open in new window

Java Developer
Top Expert 2010
Commented:
> Yes, I tried exactly that the other day, but the results were still problematic once it called on the second 'else' statement.

the approach you are taking will have problems. What a browser returns to identify itself is always changing. So you would need to be constantly updating your code to match what browsers return.

you should instead be testing for functionality provided by the browser, not what the browser is.

Author

Commented:

And how does one go about doing that?
Mick BarryJava Developer
Top Expert 2010

Commented:
depends on why you are testing, typically you do object detection.

bottom line is theres no set way to check what browser is being used

Author

Commented:

Thanks for the help. I resolved it myself.