I'm trying to make script that will redirect user depending on location. I programmed this script and geolocating works but redirecting doesn't. Why can't I redirect user?
<script>
window.onload=function(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else
{
alert("Geolocation is not supported by this browser.");
}
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.")
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.")
break;
case error.TIMEOUT:
alert("The request to get user location timed out.")
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.")
break;
}
}
if (pos.coords.longitude >= 45.775534 && pos.coords.longitude <= 45.775562 && pos.coords.latitude <= 15.994809 && pos.coords.latitude >= 15.994792)
{window.location = 'success.html';
}
else{
window.location = 'other.html';
}
</script>
In your code |pos| is not defined, you will need to define the function showPosition()
function showPosition(pos)
{
if (pos.coords.longitude >= 45.775534 && pos.coords.longitude <= 45.775562 && pos.coords.latitude <= 15.994809 && pos.coords.latitude >= 15.994792)
{
window.location = 'success.html';
}
else{
window.location = 'other.html';
}
}
Also I noticed that on Chrome, for a local file, the Geolocation does not seem to work, hence I have created a JSFIDDLE : http://jsfiddle.net/PXp9y/ and made use of alert() instead of window.location. Check it out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.