Get country code using javascript [duplicate]
Asked Answered
H

2

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.

Hermineherminia answered 14/3, 2016 at 22:24 Comment(0)
A
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);
});
Appalachian answered 14/3, 2016 at 22:29 Comment(14)
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.Hermineherminia
Remove the current $.get and replace it with my code above, if it doesn't work, does the console throw any errors?Appalachian
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.Hermineherminia
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/consoleAppalachian
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 somethingHermineherminia
change $ with jQueryAppalachian
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.Ledda
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 IPHermineherminia
Wait a sec adblock looks like it is blocking this from happening. This obviously will not work. Any other ways to do this?Hermineherminia
try disabling adblock and see if it worksAppalachian
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!Hermineherminia
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 phpAppalachian
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)Extrapolate
Note: Certain Ad Blockers prevent these types of calls from workingJuli
U
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>

Uprush answered 9/11, 2016 at 12:41 Comment(1)
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 wellGorton

© 2022 - 2024 — McMap. All rights reserved.