JSFiddle - React, Tailwind, and code Playground
HTML
Country: <span id="country"></span><br />
State/Region: <span id="stateProv"></span><br />
City: <span id="city"></span><br />
<button id="ip-nav">IP address + browser geolocation</button><br />
<button id="ip-only">IP address only</button><br />
<script src="https://cdn.db-ip.com/js/dbip.js"
data-api-key="p6bcac47ae71f0285cb6343d9697e56e41a2cb92">
</script>
JavaScript
document.getElementById("ip-nav").addEventListener("click", function() {
// Lookup visitor information using both IP address and navigator geolocation API if available
dbip.getVisitor().then(showVisitorInfo);
});
document.getElementById("ip-only").addEventListener("click", function() {
// Lookup visitor information using IP address only
dbip.getVisitor({ tryNavigatorGeolocation: false }).then(showVisitorInfo);
});
function showVisitorInfo(info) {
document.getElementById("country").innerHTML = info.countryName + " [" + info.countryCode + "]";
document.getElementById("stateProv").innerHTML = info.stateProv;
document.getElementById("city").innerHTML = info.city;
}