问题
When a user hits my website I check to see if I have a location session set in PHP. If there is no session set with the users location in I redirect them to www.domain.net/location. On this page there are a number of options for the user to pick a location. If the browser allows it one of the options is to use the browser to set this. The below code works really well.
The issue I am having is that once a user has allowed the browser to share the location this seems to be set forever. Whilst the user is browsing they may decide to change their location so they then click a button to go to the www.domain.net/location page again. Without anything popping up it automatically picks up the browsers location and redirects them. There are other location options on the page including a clickable map, I really need to be able to reset, expire, delete, force something so that the browsers asks again if the user wants to use the browser to set the location or to allow the user to use one of the other options.
The only thing I can think of is to move the actual Javascript below onto another page and on the www.domain.net/location page have another button that says something like 'Use my precise location' and that would then link to the JS below.
Does anyone know of another way of resetting this or something?
<script type="text/javascript" charset="utf-8">
function findLocation()
{
var options = {timeout: 60000};
navigator.geolocation.getCurrentPosition(foundLocation, noLocation, options);
}
function foundLocation(position)
{
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//alert('Found location: ' + lat + ', ' + lng);
window.location = "http://www.domain.net/?lat=" + lat + "&lng=" + lng;
}
function noLocation()
{
alert('Could not find location');
window.location = "http://www.domain.net/location";
}
findLocation();
</script>
回答1:
This is a per-browser setting, not something that you can control on the script side. What you may be able to do is provide a link such as this one that explains how to update geolocation settings for particular websites for the given browser.
It seems like you can get around your specific problem not by linking to /location
, but linking to /location?update
or something like that. Essentially, go to the location page but add something that prevents the automatic redirect so the user can make a different decision about their location.
回答2:
I have admittedly only played with the Javascript Location API a couple of times, but my understanding is that the browser stores User preference (this assumption seems to be reinforced here).
The cited page suggests that the preference of the user is stored on a per site (domain) basis rather than per URL. If this is not the case then adding a randomised string value to the URL should do the trick. EDIT: I have checked and the exemptions are defined per domain:
That would make your suggestion of deferring the code to another page - or binding it to an event that doesn't run on page load the best solution I can think of off-hand.
回答3:
If the user gives your site permission to use its location, it simply stores "allow" along with your site url in the browser security settings. It doesn't store the actual location. That is done via code. If you want to use a new location, just get it with javascript.
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});
See here for more on getting geolocation: https://developer.mozilla.org/en-US/docs/WebAPI/Using_geolocation
If you're storing the location in a cookie and re-using that location, just clear your cookie and reset it, or don't use cookies at all.
To see Chrome's storage of geo location settings, open your Chrome options "triple bar" > Settings > advanced... > under privacy, click Content Settings > under Location, click Manage Exceptions.
回答4:
I have decided to add ?change-location to the end of the URL and use PHP to check for this and then hide the Javascript on the page. This is not a great fix but it does look like this is a per browser setting and not something that you can reset using script. It would seem that once the user has said 'remember my preference' then thats what it will do until they dig into setting and change this on their browser.
来源:https://stackoverflow.com/questions/14956500/how-to-expire-or-reset-geolocation