这是我的代码。此功能有效,但一次只能检查一个位置。

<script>
jQuery.ajax( {
  url: '//freegeoip.net/json/',
  type: 'POST',
  dataType: 'jsonp',
  success: function(location) {
    // If the visitor is browsing from UK.
    if (location.country_code === 'UK') {
    // Redirect him to the UK Store store.
      window.location.href = 'http://www.domain.co.uk';
    }
  }
} );
</script>


这是我尝试过的:

<script>
jQuery.ajax( {
  url: '//freegeoip.net/json/',
  type: 'POST',
  dataType: 'jsonp',
  success: function(location) {
    // If the visitor is browsing from UK, Germany, France, and Sweden.
    if (location.country_code === 'UK || DE || FR || SE') {
    // Redirect him to the UK Store store.
      window.location.href = 'http://www.domain.co.uk';
    }
  }
} );
</script>


但这是行不通的。我对JavaScript的了解不是很深,所以我不确定100%如何进行这项工作。我假设我将能够沿“国家”的留置权创建一个变量(称为数组),并将其命名为与var countries = ["UK", "DE", "FR", "SE"]等国家/地区相同的名称,然后让脚本检查该人的当前位置是否为阵列中的那些国家之一。但是,我不知道该怎么做。

帮帮我!

最佳答案

location.country_code === 'UK || DE || FR || SE'


检查字符串'UK || DE || FR || SE',在您的情况下永远不会是正确的。

这将起作用:

if (location.country_code == 'UK' || location.country_code == 'DE' || location.country_code == 'FR' || location.country_code == 'SE') {
// more awesome code
}


如您所描述的,另一个方法是制作一个数组并检查条目是否存在。就像:

验证码:

var countrys = ['DE','UK','SE','FR'];
var a = 'DE';
if (a.indexOf(countrys)) {
    alert('It is');
}

09-25 16:36
查看更多