Get country code using javascript

38,614

Solution 1

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);
});

Solution 2

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>

Share:
38,614
702cs
Author by

702cs

Updated on November 10, 2020

Comments

  • 702cs
    702cs over 3 years

    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.

  • 702cs
    702cs about 8 years
    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.
  • Xposedbones
    Xposedbones about 8 years
    Remove the current $.get and replace it with my code above, if it doesn't work, does the console throw any errors?
  • 702cs
    702cs about 8 years
    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.
  • Xposedbones
    Xposedbones about 8 years
    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? developer.chrome.com/devtools/docs/console
  • 702cs
    702cs about 8 years
    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
  • Xposedbones
    Xposedbones about 8 years
    change $ with jQuery
  • JanR
    JanR about 8 years
    You need to include jQuery on your page: add <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script> at the top of the page.
  • 702cs
    702cs about 8 years
    Got past the Uncaught Reference, now I'm getting: GET ipinfo.io/xx.xx.xx.xx net::ERR_BLOCKED_BY_CLIENT xx's is my IP
  • 702cs
    702cs about 8 years
    Wait a sec adblock looks like it is blocking this from happening. This obviously will not work. Any other ways to do this?
  • Xposedbones
    Xposedbones about 8 years
    try disabling adblock and see if it works
  • 702cs
    702cs about 8 years
    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!
  • Xposedbones
    Xposedbones about 8 years
    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
  • roberto tomás
    roberto tomás over 7 years
    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)
  • Albert Renshaw
    Albert Renshaw over 5 years
    Note: Certain Ad Blockers prevent these types of calls from working
  • Prem Santh
    Prem Santh almost 4 years
    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 ip2c.org - more reliable and its FREE as well