1
<!DOCTYPE html>
<html>
<head>
<title>GEOIP DB - jQuery example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div>Country: <span id="country"></span>
<div>State: <span id="state"></span>
<div>City: <span id="city"></span>
<div>Latitude: <span id="latitude"></span>
<div>Longitude: <span id="longitude"></span>
<div>IP: <span id="ip"></span>
<script>
$.ajax({
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function( location ) {
$('#country').html(location.country_name);
$('#state').html(location.state);
$('#city').html(location.city);
$('#latitude').html(location.latitude);
$('#longitude').html(location.longitude);
$('#ip').html(location.IPv4);
}
{
"Los Angeles": "http://exemplo.com/LA",
"New York": "http://exemplo.com/NY",
"Other City": "http://exemplo.com/othercity"
}
});
</script>
</body>
</html>
Example: Access http://Exemplo.com get the city "New York" redirects to http://Exemplo.com/NY or if it’s "Los Angeles" redirect to http://Exemplo.com/LA? And if it were any other city, it would go to a third URL. Is there any way I could do that I tried and I couldn’t.
What you’ve tried, that you haven’t succeeded in
– Alex
Add routes to a variable. Ex:
routes = {"cidade1": "http://rota1.com", "cidade2": "http://rota2.com"}
etc. Then just take the value oflocation.city
and check if there isarray
created. Ex:if ( routes[location.city] ) { window.location.href = routes[location.city] }
– Valdeir Psr
Thanks Valdeir tried, but I get no results. I’m beginner in jquery.
– Robsonrrn