Would Javascript be good for this? Can JavaScript even do this? If Javascript is not good for this purpose, how about PHP?
Asked
Active
Viewed 377 times
0
-
1you could issue an AJAX request to one of the IP geolocation services. – Stephen Thomas Mar 18 '14 at 00:28
-
There is `navigator.language` which gives you the user's locale. You can't access user's IP address with JavaScript, if that's what you were thinking. – sabof Mar 18 '14 at 00:29
-
Does this question's answer help? http://stackoverflow.com/questions/3489460/how-to-get-visitors-location-i-e-country-using-javascript-geolocation – Fizz Mar 18 '14 at 00:30
-
@sabof language != country! – epascarello Mar 18 '14 at 00:30
-
@epascarello I never said it was. – sabof Mar 18 '14 at 00:32
-
What about the [geolocation API](https://developer.mozilla.org/en-US/docs/WebAPI/Using_geolocation)? – Pointy Mar 18 '14 at 00:35
2 Answers
1
jQuery version. conversion to standard Javascript is straightforward
$.get("http://ip-api.com/json", function(response) {
console.log(response.country);
});

Stephen Thomas
- 13,843
- 2
- 32
- 53
0
You could use http://ipinfo.io API for this.
$.get("http://ipinfo.io", function (response) {
console.log(response.country)
}, "jsonp");

Chris
- 1,020
- 10
- 19