const lookupElem = document.getElementById('lookupElem'); const yourIp = document.getElementById('your-ip'); function getFlagEmoji(countryCode) { return countryCode .toUpperCase() .replace(/./g, char => String.fromCodePoint(127397 + char.charCodeAt())); } function fetchIp() { const ip = document.getElementById("ipInput").value; fetch(`https://ipapi.co/${ip}/json/`) .then(response => response.json()) .then(data => { const flag = getFlagEmoji(data.country_code || ''); const table = `
IP${data.ip}
City${data.city}
Postal Code${data.postal}
Region${data.region}
Country${flag} ${data.country_name} (${data.country_code})
Timezone${data.timezone}
Organization${data.org}
Coordinates${data.latitude}, ${data.longitude}
`; document.getElementById("output").innerHTML = table; }) .catch(err => { document.getElementById("output").textContent = "Error: " + err; }); } function getYour() { fetch(`https://ipapi.co/json/?ip=ipv4`) .then(response => response.json()) .then(data => { yourIp.innerHTML = data.ip; }) .catch(err => { yourIp.textContent = "Error: " + err; }); } window.addEventListener('load', getYour); lookupElem.addEventListener('click', fetchIp);