...

View Full Version : Redirect in a if-statement doesn't work



kungpung
07-15-2010, 02:48 AM
I have a page with a Geo IP redirect that's supposed to redirect users from London to URL#1 and the rest to URL#2. It's an external geo ip lookup service.


First comes the IP lookup:

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js">
/*
GeoIP Deny Access by City and Redirect Javascript 1.0
http://wiki.category5.tv/MaxMind_GeoIP_API

API (c) MaxMind - www.maxmind.com - used with permission

"GeoIP Deny Access by City" script by Robbie Ferguson, www.Category5.TV

You are free to use and share this script, however this notice must remain intact.
*/
</script>



And then, and here's the problem I think, is the redirects inside an if/else:


<script type="text/javascript">


var city=new Array("London, H9")

var redirect="http://www.URL1.com"
var redirect2="http://www.URL2.com"


/* do not edit past this line */
Array.prototype.inArray = function(q) {
for(i in this) { if(this[i].toUpperCase() === q) return true; }
}
var myCity=geoip_city().toUpperCase()
var myRegion=geoip_region().toUpperCase()
if(city.inArray(myCity+", "+myRegion))
{
window.location = redirect;
}
else
{
window.location = redirect2;
}



The redirect works if you are indeed from London. So if the if-statement is true, "window.location = redirect" works, but if the statement is not true, "window.location = redirect2" doesn't seem to be called.


Help would be extremely appreciated :)

kungpung
07-15-2010, 05:38 PM
I guess the real question could be simplified into this:

Does this code work, or is it not allowed for some reason?


if(city.inArray(myCity+", "+myRegion))
{
window.location = redirect;
}
else
{
window.location = redirect2;
}

Old Pedant
07-15-2010, 07:19 PM
Looks okay to me.

But why not debug and find out??

If you don't know how to use FireBug, then at a minimum you could add some alert's in there.

Maybe this:


alert("city ::" + city + "::\nmyCity ::" + myCity + "::\nmyRegion ::" + myRegion + "::");
if(city.inArray(myCity+", "+myRegion))
{
alert("ready to do redirect to " + redirect);
window.location = redirect;
}
else
{
alert("ready to do redirect to " + redirect2);
window.location = redirect2;
}
alert("How did we get here????");

kungpung
07-16-2010, 12:00 AM
OK I've done some more testing with alerts and the problem lies in this line:



if(city.inArray(myCity+", "+myRegion))

If the conditions are met, it calls the lines it should, but "else" doesn't do anything if the conditions are not met. Not sure why, but I don't really understand what inArray returns either..

I also tried doing another if-statement instead of "else":


if(!(city.inArray(myCity+", "+myRegion)))

But that didn't work either, nothing was called.

Old Pedant
07-16-2010, 01:03 AM
Oh for crying out loud!!!

YES! The inArray function is BROKEN!


Array.prototype.inArray = function(q) {
for(i in this) { if(this[i].toUpperCase() === q) return true; }
return false;
}

Try adding that missing line!!!

The function was returning null, and null is *neither* true nor false!

Old Pedant
07-16-2010, 01:04 AM
Good catch, trying

if(!(city.inArray(myCity+", "+myRegion)))

That ! giving no result is what made me go look at the function.

kungpung
07-16-2010, 02:51 AM
Awesome :)


Thanks a million times for the help, you're a credit to humanity!!

randomuser773
07-16-2010, 02:55 AM
The function was returning null, and null is *neither* true nor false!
null tests false, but strictly the function was returning undefined, which also tests false.

kungpung
07-16-2010, 03:20 AM
I tried adding the suggested line to the function, but it still doesn't work. Hmm..

Shouldn't


if((city.inArray(myCity+", "+myRegion)) == null)

be called if inArray is


Array.prototype.inArray = function(q) {
for(i in this) { if(this[i].toUpperCase() === q) return true; }
}






I also tried

if(!(city.inArray(myCity+", "+myRegion)))
and doing a regular if+else with the suggested added line (return false) to the function but that didn't help either.

randomuser773
07-16-2010, 03:44 AM
Array.prototype.inArray = function(q) {
for(i in this) { if(this[i].toUpperCase() === q) return true; }
}
That algorithm doesn't even execute, since it picks up other properties of the array. It generates console errors that you should have seen.


Array.prototype.inArray = function(q)
{
for( var i = 0, rv = false ; i < this.length && !rv; i++ )
rv = ( this[i].toUpperCase() === q );

return rv;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum