Loading ...

Get country from IP | CodeAsp.Net

Get country from IP

 /5
0 (0votes)

Below is the simple script to get country from IP:

 $.get("http://ipinfo.io/"+ip, function (response) {
        //response.country);
        //response.region);
    }, "jsonp");

Here is how you can use it:

Html:

<table class="table">
  <tr>
    <th>IP</th>
    <th>Country</th>
    <th>Region</th>
  </tr>
  <tr class="row">
    <td class="ip">203.146.82.253</td>
    <td class="country"></td>
    <td class="region"></td>
  </tr>
  <tr class="row">
    <td class="ip">122.72.33.139</td>
    <td class="country"></td>
    <td class="region"></td>
  </tr>    
  <tr class="row">
    <td class="ip">77.66.201.163</td>
    <td class="country"></td>
    <td class="region"></td>
  </tr>    
  <tr class="row">
    <td class="ip">89.248.112.194</td>
    <td class="country"></td>
    <td class="region"></td>
  </tr>    
</table>

JavaScript:

$('tr.row').each(function(i){
    console.log(this);
    var self = $(this);
    var ip = self.find('td.ip');
    var country=self.find('td.country');
    var region=self.find('td.region');
    
    $.get("http://ipinfo.io/"+ip.text(), function (response) {
        country.html(response.country);
        region.html(response.region);
    }, "jsonp");
});

Output:


IP                       Country     Region
203.146.82.253  TH          Krung Thep
122.72.33.139     CN         Beijing
77.66.201.163     RU         Krasnodar
89.248.112.194   RU         Tatarstan

Live Demo: http://jsfiddle.net/raghav_khunger/p26uE/115/

Comments (no comments yet)

Top Posts