5

我想做的是根据国家代码和下面的脚本重定向国家。下面的代码不起作用。做一些研究我发现我必须使用 or 语句,或者至少这是我认为我需要的,但我的问题是有比 or 语句更简单的方法吗?如您所见,我正在检查很多国家/地区。

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

2 回答 2

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)
{
    ...
}
于 2013-10-08T04:55:16.053 回答
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
}

但是客户端很容易伪造其来源。

于 2013-10-08T05:07:39.653 回答