-4

I want to show an entry popup only to those who are visiting from India using JavaScript.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    You need to give more detail when asking questions and describe what you have already tried. – Philip Deatherage Aug 01 '17 at 10:20
  • Possible duplicate: https://stackoverflow.com/questions/6747833/how-can-i-find-a-user-s-country-using-html5-geolocation (and numerous duplicates linked from there) – freedomn-m Aug 01 '17 at 10:39

2 Answers2

1

Firstly get the IP of the visitor

$(document).ready(function () {
    $.getJSON("http://jsonip.com/?callback=?", function (data) {
        console.log(data);
        alert(data.ip);
    });
});

Then use a service that actually returns country names, like FreeGeoIp

$.getJSON("http://freegeoip.net/json/", function (data) {
    var country = data.country_name;
    var ip = data.ip;
});

After that shw your popup

if(country == 'India') {
    //......
}
Munawir
  • 3,346
  • 9
  • 33
  • 51
  • Thanks for the prompt reply Munawir. – Mahesha N C Aug 01 '17 at 10:45
  • 1
    this works for me $(window).load(function () { $.get("http://ipinfo.io", function (response) { var country = response.country; //alert(country); if (country == 'IN') { $("#myModal").modal({ backdrop: 'static', keyboard: false }); } }, "jsonp"); }); – Mahesha N C Aug 01 '17 at 11:14
-1

You need to do IP Geo-location. There are a quite a few plugins that will help with this, find the one that best suits your application.

Munawir
  • 3,346
  • 9
  • 33
  • 51
Difster
  • 3,264
  • 2
  • 22
  • 32