簡體   English   中英

基於國家/地區的簡單jQuery重定向無法正常工作

[英]simple jquery redirection based on country not working properly

您能讓我理解為什么此代碼似乎無法正常工作嗎?

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

正確地說,我的意思是即使我有GB或RO或US或CA或任何其他國家,這也將我重定向到shop-in-canada.myshopify.com。 您認為問題出在哪里?

if (location.country_code === 'RO' || 'GB')

不行 記錄該行,您不會得到truefalse ,而是獲得了"GB"

只需將以上內容替換為

if (location.country_code === 'RO' || location.country_code === 'GB')

會成功的

我認為你應該更換

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';
}

好了,您可以嘗試在每個國家/地區代碼都必須做的地方嘗試這種方式。

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';
                    }
                }
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM