你可以让我理解为什么这段代码似乎不能正常工作吗?
<html><head><title></title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
// If the visitor is browsing from Romania or GB
if (location.country_code === 'RO' || 'GB') {
// Redirect him to the Canadian store.
window.top.location.href = 'http://shop-in-canada.myshopify.com';
}
else
{ return false; }
}
} );
</script>
</head><body></body></html>
正确地说,这意味着我将这个重定向到shop-in-canada.myshopify.com,即使我有GB或RO或US或CA或任何其他国家。您认为问题来自哪里?
答案 0 :(得分:1)
if (location.country_code === 'RO' || 'GB')
赢了工作。记录该行,您不会返回true
或false
,而是获得"GB"
。
只需用
替换上面的内容即可 if (location.country_code === 'RO' || location.country_code === 'GB')
会做到这一点。
答案 1 :(得分:0)
我认为你应该替换
if (location.country_code === 'RO' || 'GB') {
// Redirect him to the Canadian store.
window.top.location.href = 'http://shop-in-canada.myshopify.com';
}
到
if (location.country_code === 'RO' || location.country_code === 'GB') {
// Redirect him to the Canadian store.
window.top.location.href = 'http://shop-in-canada.myshopify.com';
}
要进行多项检查,请执行
var language = ["RO", "GB"];
if (language.indexOf(location.country_code) !== -1) {
// Redirect him to the Canadian store.
window.top.location.href = 'http://shop-in-canada.myshopify.com';
}
答案 2 :(得分:0)
嗯,你可以尝试这种方式,你必须为每个国家代码。
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
if (location.country_code === 'US') {
// do nothing
}
else if (location.country_code === 'AU') {
window.location.href = 'http://yoururlhere.com';
}
else if (location.country_code === 'SE') {
window.location.href = 'http://yoururlhere.com';
}
else if (location.country_code === 'NL') {
window.location.href = 'http://yoururlhere.com';
}
else if (location.country_code === 'GB') {
// window.location.href = 'http://yoururlhere.com';
}
}
});