user2716614
user2716614

Reputation: 53

Alternative to OR statement

What I am trying to do is redirect countries, based on country code with the script below. The code below does not work. Doing some research I found I have to use an or statement or at least that's what I think I need, but my question is there an easier way then an or statement? As you can see there are a lot countries I am checking for.

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">

var country= geoip_country_code();

if(country = "UK","CA","DE","DK","FR","AU","SE","CH","NL","IT","BE","AT","ES","NO","IE","FI","GB","US")      
{
window.location.href='http://www.google.com'; 
}
else   
{
window.location.href='http://www.yahoo.com'; 
}
</script>

Upvotes: 5

Views: 128

Answers (2)

RobG
RobG

Reputation: 147363

You can do something like:

var country = 'US';
var countries = 'UK CA DE DK FR AU SE CH NL IT BE AT ES NO IE FI GB US';

if (countries.match(country.toUpperCase())) {
  // matched
} else {
  // no match
}

But it's very easy for the client to spoof its origin.

Upvotes: 0

Ben S.
Ben S.

Reputation: 1143

You should be able to do it like this:

var countryCodes = ["UK","CA","DE","DK","FR","AU","SE","CH","NL","IT","BE","AT","ES","NO","IE","FI","GB","US"];
var country= geoip_country_code();
if (countryCodes.indexOf(country) !== -1)
{
    ...
}

Upvotes: 12

Related Questions