10

I can't figure out how to get a country code from a visitor that goes to my webpage. I have seen plenty of other examples but none use plain Javascript. Here is my current code:

<!DOCTYPE html>
<html lang="en-US">
<head>
</head>
<body>
    <script type="text/javascript">
        var userip;
    </script>
    <script type="text/javascript" src="https://l2.io/ip.js?var=userip">       </script>

    <script type="text/javascript">
        document.write("IP: ", userip);

        $.get("http://ipinfo.io/"+userip.text(), function (response) {
            reg.text("country code here" + response.country);
        }, "jsonp");
    </script>
</body>
</html>

I get the user's IP addrress from a JSONP request to ipinfo.io using the $.get method of jQuery.

Unfortunately l2.io.js does not return anything other then the ip at this time.

If anyone can help or has any examples please link all related JavaScript libraries that will be needed. Thanks.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
702cs
  • 331
  • 1
  • 4
  • 13

2 Answers2

11

According to the docs on the website, you should be able to retrieve the country code with ipinfo. try using $.getJSON instead of $.get

var country_code = null;
$.getJSON('http://ipinfo.io/' + userip, function(data){
    country_code = data.country;
    alert(country_code);
});
Xposedbones
  • 597
  • 4
  • 7
  • I must not understand javascript at all. I added exactly that underneath my current $.get. Do I need to include a json .js? do I need to add any code breaks or ;'s to this? Maybe this is working so how can I turn data.country into a variable within javascript? and will document.write(variable); actually work? I'm so lost and confused sorry. – 702cs Mar 14 '16 at 22:36
  • Remove the current $.get and replace it with my code above, if it doesn't work, does the console throw any errors? – Xposedbones Mar 14 '16 at 22:43
  • I'm literally running all this from a .html file and notepad so I'm not sure were I can read the console or see it. Sorry! I also replaced my current $.get with yours and tried to do a document.write(data.country) and document.write(data.country.text()) still nothing. – 702cs Mar 14 '16 at 22:45
  • Right click anywhere on the page, click on "Inspect Element" and then there will be a tab called Console. Is there something red written in this tab? https://developer.chrome.com/devtools/docs/console – Xposedbones Mar 14 '16 at 22:48
  • Uncaught ReferenceError: $ is not defined - btw thanks for this I had no clue console.log was actually the browsers debug console. I'm going to try adding in jquery 2.2 and json libs or something – 702cs Mar 14 '16 at 22:50
  • change $ with jQuery – Xposedbones Mar 14 '16 at 22:51
  • You need to include jQuery on your page: add `` at the top of the page. – JanR Mar 14 '16 at 22:56
  • Got past the Uncaught Reference, now I'm getting: GET http://ipinfo.io/xx.xx.xx.xx net::ERR_BLOCKED_BY_CLIENT xx's is my IP – 702cs Mar 14 '16 at 23:00
  • Wait a sec adblock looks like it is blocking this from happening. This obviously will not work. Any other ways to do this? – 702cs Mar 14 '16 at 23:02
  • try disabling adblock and see if it works – Xposedbones Mar 14 '16 at 23:07
  • It's disabled and works perfectly. Gonna use this for now but after thinking about this I believe to achieve this without getting blocked is querying a database. But thanks again for the help this works! – 702cs Mar 14 '16 at 23:14
  • Doing it that way will almost always be block since it relies on external site retrieving your ip. the best way to do it would be with php – Xposedbones Mar 14 '16 at 23:16
  • this doesn't accurately provide the right code though — for example you will get the literal country "PR" for puerto rico (correct), but also "GB" for the united kingdom (arguably incorrect — technically, provinces in the UK are "countries" and so this is sorta right, but it is not the country code, it is the provincial-level code) – roberto tomás Dec 16 '16 at 15:10
  • Note: Certain Ad Blockers prevent these types of calls from working – Albert Renshaw Aug 11 '18 at 22:48
6

I have used the below code using jQuery and its working fine for me. I am getting the country_code , country_name etc.,.

 jQuery(document).ready(function($) {
    jQuery.getScript('http://www.geoplugin.net/javascript.gp', function() 
    {
        var country = geoplugin_countryName();
        alert(country);
        var code = geoplugin_countryCode();
        alert(code);
        console.log("Your location is: " + country + ", " + zone + ", " + district);
    });
});

Note: Do remember to import the plugin script as below:

<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>

Mehnaz Saheb
  • 89
  • 1
  • 4
  • Fails sometimes, I used a VPN - pointing to US and tried the above API , It returned JAPAN. ipinfo.io and others says it united states I found https://ip2c.org/ - more reliable and its FREE as well – Prem Santh Jun 07 '20 at 06:49