使用HTML和Javascript查找三个字母的国家/地区代码 [英] Find the three letter country code using HTML and Javascript
问题描述
我正试图找出用户在哪个国家/地区浏览的位置。我需要为此使用HTML和Javascript。我在这里看到了stackoverflow上的各种帖子,其中有人建议使用不同的API。我最感兴趣的是任何免费的API,并且可以给我三个字母的国家代码而不是两个。任何其他API对我来说都没问题,我不需要使用IPInfo,我也可以使用geonames。
在查找用户的国家/地区代码后,我需要制作一个这样的URL,假设countryCode是美国,那么它应该是这样的 -
some_url& countryCode = USA $如果countryCode是IND,那么它应该是这样的 -
c> some_url& countryCode = IND
我有下面的代码可以找到currentLocation,但countryCode只有两个字母。我需要三个字母的国家/地区代码,然后相应地创建网址。
< html>
< head>
< title>获取网页访问者的位置< / title>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>< / script>
< script>
$ b $ .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 />完整回复:< pre id =details>< / pre>
< a href =url& country = countryCode> url< / a>
< / body>
< / html>
任何人都可以帮我解决这个问题吗?
ipinfo.io
does NOT 提供三个字母的国家/地区代码。您必须手动生成一个数组:
var threeLtrCC.US ='USA';
var threeLtrCC.IN ='IND';
...
$ .get(http://ipinfo.io,function(response){
var cc = threeLtrCC [response.country];
$( '#newURL')。text('some_url?countryCode ='+ cc);
$('#newURL')。attr('href','some_url?countryCode ='+ cc);
$(#details)。html(JSON.stringify(response,null,4));
},jsonp);
,然后使用 response.country
作为键找到您自制数组的值。
HTML
< a id =newURL>< / a>
I am trying to find user's location meaning in which country he is browsing. I need to use HTML and Javascript for this. I have seen various posts on stackoverflow on this in which people have suggested to use different different API's. I am mostly interested in any free API's and which can give me three letter country code not two. Any other API's is fine with me, it's not mandatory that I need to use IPInfo, I can als use geonames as well.
And after finding the user's country code, I need to make a URL like this, suppose the countryCode is 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 below code with me which will find the currentLocation but the countryCode is of two letter only. I need three letter country code and then make the url accordingly.
<html>
<head>
<title>Get web visitor's 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?
ipinfo.io
does NOT provide a three-letter country code. You'll have to manually produce an array:
var threeLtrCC.US = 'USA';
var threeLtrCC.IN = 'IND';
...
$.get("http://ipinfo.io", function (response) {
var cc = threeLtrCC[response.country];
$('#newURL').text('some_url?countryCode=' + cc);
$('#newURL').attr('href','some_url?countryCode=' + cc);
$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
and then use response.country
as the key to find the value from your homemade array.
HTML
<a id="newURL"></a>
这篇关于使用HTML和Javascript查找三个字母的国家/地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!