Как только вы определили страну, вы можете перейти к внешнему API, например https://restcountries.eu
, и получить данные (включая код телефона) о любой стране.
Например, https://restcountries.eu/rest/v2/name/nepal?fullText=true
возвращает этот JSON, где вы можетесм. callingCodes = 997
.
[{
"name": "Nepal",
"topLevelDomain": [".np"],
"alpha2Code": "NP",
"alpha3Code": "NPL",
"callingCodes": ["977"],
"capital": "Kathmandu",
"altSpellings": ["NP", "Federal Democratic Republic of Nepal", "Loktāntrik Ganatantra Nepāl"],
"region": "Asia",
"subregion": "Southern Asia",
"population": 28431500,
"latlng": [28.0, 84.0],
"demonym": "Nepalese",
"area": 147181.0,
"gini": 32.8,
"timezones": ["UTC+05:45"],
"borders": ["CHN", "IND"],
"nativeName": "नेपाल",
"numericCode": "524",
"currencies": [{
"code": "NPR",
"name": "Nepalese rupee",
"symbol": "₨"
}],
"languages": [{
"iso639_1": "ne",
"iso639_2": "nep",
"name": "Nepali",
"nativeName": "नेपाली"
}],
"translations": {
"de": "Népal",
"es": "Nepal",
"fr": "Népal",
"ja": "ネパール",
"it": "Nepal",
"br": "Nepal",
"pt": "Nepal",
"nl": "Nepal",
"hr": "Nepal",
"fa": "نپال"
},
"flag": "https://restcountries.eu/data/npl.svg",
"regionalBlocs": [{
"acronym": "SAARC",
"name": "South Asian Association for Regional Cooperation",
"otherAcronyms": [],
"otherNames": []
}],
"cioc": "NEP"
}]
РЕДАКТИРОВАТЬ: Код добавлен по запросу.
<html>
<body>
<h2>Get the phone code for Napal</h2>
<button type="button" onclick="loadDoc()">Get Phone Code</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var locObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = "Phone Code for Napal = "+locObj[0].callingCodes;
}
};
xhttp.open("GET", "https://restcountries.eu/rest/v2/name/nepal?fullText=true", true);
xhttp.send();
}
</script>
</body>
</html>
Производит этот вывод:
Phone Code for Napal = 977