Reputation: 15
can you make me understand why this code seems to not work 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>
By properly i mean that this redirects me on shop-in-canada.myshopify.com even i have GB or RO or US or CA or any other country. Where do you think the problem is coming from?
Upvotes: 0
Views: 1006
Reputation: 3261
I think you should replace
if (location.country_code === 'RO' || 'GB') {
// Redirect him to the Canadian store.
window.top.location.href = 'http://shop-in-canada.myshopify.com';
}
to
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';
}
For multiple check, do
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';
}
Upvotes: 0
Reputation: 1102
Well, you can try this way where you have to do for each country code.
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';
}
}
});
Upvotes: 0
Reputation: 124
if (location.country_code === 'RO' || 'GB')
Won't work. Logging that line, you don't get true
or false
back, but instead you get "GB"
.
Simply replacing the above with
if (location.country_code === 'RO' || location.country_code === 'GB')
will do the trick.
Upvotes: 1