OR语句的替代方案

时间:2013-10-08 04:51:41

标签: javascript

我要做的是重定向国家/地区,根据国家/地区代码和下面的脚本。下面的代码不起作用。做一些研究我发现我必须使用或声明或至少我认为我需要的,但我的问题是有一个更容易的方式然后或声明?如您所见,我正在检查的国家很多。

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

2 个答案:

答案 0 :(得分:12)

你应该能够这样做:

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)
{
    ...
}

答案 1 :(得分:0)

您可以执行以下操作:

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
}

但是客户很容易欺骗它的起源。