Geolocation API in Safari 8 and 7.1 keeps asking permission

5.9k views Asked by At

I've built the Geolocation API into my webapp and all browsers are working fine. Except Safari 8 and 7.1. The browser keeps asking for permission after allowing or not allowing it and ends up in an infinite loop making the browser (tab) unusable. It's easily reproducible by just going to http://html5demos.com/geo in Safari.

Is there any fix for this or is this just a bug in Safari? I searched but couldn't find anything related.

enter image description here

3

There are 3 answers

6
Chris Camaratta On BEST ANSWER

Our team saw this too. At first we thought we might have been stuck inside some kind of loop that the other browsers ignored, so we instrumented the code and confirmed that the call was only happening once. We suspended JavaScript by forcing a breakpoint and the alerts kept coming (and coming, and coming...). At this point we were pretty sure the issue was not in our code.

On a whim I placed the call to the GeoLocation API inside a setTimeout (to allow the call stack to empty) and the problem went away. No idea why that would fix the issue. . .

Edit

Per request, I put up an example @ https://jsfiddle.net/r8hst2zp/1/

2
MeMTn On

i don't think it is a bug in safari, you can try the following example, it worked fine for me:

http://www.w3schools.com/html/html5_geolocation.asp

1
stianlp On

Ok. So from the two answers from @MeMTn and @chris-camaratta, here is a few thoughts:

1)

On a whim I placed the call to the GeoLocation API inside a setTimeout (to allow the call stack to empty) and the problem went away. No idea why that would fix the issue. . .

I did the same in my angularjs app, and it works. Perfect!

2)

i don't think it is a bug in safari, you can try the following example, it worked fine for me: http://www.w3schools.com/html/html5_geolocation.asp

How come this works (in Safari!) without setTimeout?

In my angularjs application, the call to GeoLocation is one of the first things to happen when the page loads. I believe this is the case for almost all other applications/web pages. In the W3 example there is a clickable button that fires the call to GeoLocation when the user clicks it (which happens when the page has loaded).

By setting a timeout, we allow Safari to deal with some other stuff (that might have something to do with calling the GeoLocation API for all I know) for a few (milli)seconds.

If I set the timeout to < 10 the error is back, even on localhost. I think I will stick with 100 ms for now. But that is highly unfair for other, working browsers.

Bug or not it's clearly something with Safari.