How do I handle when a user closes the Physical Location prompt in Firefox and Chrome?

I am using a Geolocation object and getCurrentPosition ().

Have you seen that every time you use getCurrentPosition Firefox and Chrome, tell me these massage sessions?

Chrome:

Example.com wants to track your physical location [allow] [deny] [ X close ] 

Firefox:

 Example.com wants to know your location [Share Location] [Don't Share] [Close] 

My question is: how to handle when the user does not specify the "Allow" or "Deny" option, but closes the invitation instead?

I have this JAVASCRIPT code, but it does not work when the user closes the request:

  if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( onSuccess, onError, { //enableHighAccuracy: true, timeout: 5000 //maximumAge: 120000 } ); alert("holaaaa"); } else { alert('Geolocation not supported.'); } 

Am I having problems with this, please help?

This question is related to this post: How to call a function when the user allows or denies access to the "Physical location"?

+7
source share
1 answer

The spec is actually pretty quiet as to whether the callback should be executed if the user simply rejects the permission request (as opposed to clicking the Reject button):

For getCurrentPosition and watchPosition implementation should never call successCallback without first obtaining permission from the user to exchange the location. In addition, an implementation must always obtain user permission to share a location before performing any getCurrentPosition or watchPosition steps described above. If the user grants permission, you must call the appropriate callback, as described above. If the user denies permission, errorCallback (if any) must be called with the PERMISSION_DENIED code , regardless of any other errors encountered in the above steps. The time taken to obtain user permission should not be included in the period covered by the timeout attribute of the PositionOptions parameter. The timeout attribute should only apply to a location operation.

I would say that rejecting a permission request is equivalent to rejecting the request in terms of getCurrentPosition . Clicking Deny is explicit, while dismissal (clicking on X) is implicit.

Functionally, the difference is that clicking the Deny button means (1) rejecting the current request and (2) the site is blacklisted and the user will never be asked to exchange the location for the site again; just rejecting the request means only that the current request is rejected - ndash; future requests (in the future BROWSINGCONTEXT, after reloading the page, a request appears again.

In my opinion, the current behavior of Chrome, Firefox and IE is a mistake - all three do nothing if the user rejects the permission request by clicking X. When calling the geolocation errorCallback , errorCallback should be called, because any further getCurrentLocation calls in the current BROWSINGCONTEXT will silently fail. (Opera 11.52 behaves "correctly" because its permission request user interface has only Allow / Deny buttons, not X.)

Mozilla has a bug where they say that the current behavior is correct and a new bug open an error that is relevant; Chromuim also says the current behavior is correct. IE has an error (registration is required), but, oddly enough, Microsoft closed it as unplayable.

This is really what the W3C working group should address. The page should be able to respond to the user by rejecting the placement permission request - whether it is permanently refused or simply rejected for this session by pressing the X button.


The only thing you can do right now to solve this problem is to set your own timeout. Set the timeout in the geolocation request, and then set the response timeout to a longer time (so that the user time will respond to the request):

 if (navigator.geolocation) { var etimeout = setTimout(onError, 10000); navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout:5000}); } function onSuccess(pos) { clearTimeout(etimeout); // ... } function onError(err) { clearTimeout(etimeout); // Note `err` will actually be undefined if this is the result of our timeout // and anything you do here should be undone by `onSuccess` (in case the user // took longer than 5 seconds to approve the request; for that reason, you // shouldn't display any modal UI from here. // ... } 
+15
source

All Articles