I am trying to find the value of the location of the user in which country he is viewing. For this I need to use HTML and Javascript. I saw various posts on stackoverflow where people suggested using different APIs. What interests me most is any free API, and which can give me three-letter code, not two. Any other API is fine with me, it is not necessary that I need to use IPInfo, I can also use geonames.
And after finding the user’s country code, I need to make such a URL, suppose countryCode is the USA, then it should be like this:
some_url&countryCode=USA
if countryCode is IND, then it should be like this:
some_url&countryCode=IND
I have the code below with me that will find currentLocation, but countryCode has only two letters. I need a country code with three letters, and then enter the URL accordingly.
<html>
<head>
<title>Get web visitor location</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$.get("http://ipinfo.io", function (response) {
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
</script>
</head>
<body>
<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
<a href="url&country=countryCode">url</a>
</body>
</html>
Can anyone help me with this?
source
share