Reputation: 31
I would like to redirect visitors from and to a specific page within my website if those visitors are from any other country besides the United States, using freegeoip.net
I am a relative beginner to coding and I wasn't able execute the code on this page and can't figure out why: Redirect all countries except ONE
If anybody has a complete code or specific instructions as to what I need to do EXACTLY to execute this code... I would be extremely grateful... I have literally been spending day and night these past four days, on my "vacation", trying to figure this out and I have completely given up on my abilities.
So if anybody can give me the code, please explain it to me as if I were a middle school student... Thank you so much.
Upvotes: 3
Views: 1613
Reputation: 93
I know this may be a bit late of an answer. Here is the some working code. You can use an AJAX call and set conditions based on the data returned.
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: 2