Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Redirect in a if-statement doesn't work

    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:
    Code:
    <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:

    Code:
    <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

  • #2
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I guess the real question could be simplified into this:

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

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

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,561 Times in 4,525 Posts
    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:
    Code:
    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????");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    OK I've done some more testing with alerts and the problem lies in this line:


    Code:
    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":

    Code:
    if(!(city.inArray(myCity+", "+myRegion)))
    But that didn't work either, nothing was called.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,561 Times in 4,525 Posts
    Oh for crying out loud!!!

    YES! The inArray function is BROKEN!
    Code:
    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!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    kungpung (07-16-2010)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,561 Times in 4,525 Posts
    Good catch, trying
    Code:
    if(!(city.inArray(myCity+", "+myRegion)))
    That ! giving no result is what made me go look at the function.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome


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

  • #8
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by Old Pedant View Post
    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.

  • #9
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I tried adding the suggested line to the function, but it still doesn't work. Hmm..

    Shouldn't

    Code:
    if((city.inArray(myCity+", "+myRegion)) == null)
    be called if inArray is

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





    I also tried
    Code:
    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.

  • #10
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by kungpung View Post
    Code:
    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.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •