15

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

Gregory Bolkenstijn
  • 10,003
  • 7
  • 36
  • 38
  • I have two confirm dialogs - one from browser, one from system. While system is open - browser don't close. If I agree in system dialog - I can close the browser. Looks like a bug. – Alex Dec 03 '14 at 12:51
  • I think it's a bug as well. Maybe they fixed it in Yosemite? – www139 Dec 03 '14 at 14:04
  • I'm experiencing this too and used the W3 school example. It'll request my location up to four times, but it's only ever the last input that takes effect. Also wrapped my function in jQuery's `one()` in an attempt to solve the problem. – bholtbholt Dec 05 '14 at 20:44
  • I'm getting this bug also, can't seem to get around this. – Nick M Jan 19 '15 at 01:46

3 Answers3

26

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/

Chris Camaratta
  • 2,729
  • 22
  • 35
  • 1
    Came here because of a search for this issue, and this absolutely fixed the problem. Thanks! – saikofish Feb 13 '15 at 05:08
  • 1
    Ditto. I've struggled with this bug for a while (which at first was hidden behind another bug) and could never reproduce it. Turns out it was it - a team member had it happening live, so I piggybacked on it and saw that behaviour. This answer cleared it. Note that it doesn't always happen, either... – Sébastien Renauld Apr 28 '15 at 15:03
  • Is there any better solution than this. Why hasn't safari fixed this yet. – David May 31 '15 at 22:12
  • @David Because Safari suffers from the same thing all Apple products do - [bare minimum bugfixing, improvement lifecycle or attention to issues](http://nolanlawson.com/2015/06/30/safari-is-the-new-ie/) unless forced to. – dKen Dec 21 '15 at 08:16
  • @Chris Hi mate, are you able to put up a quick example of how you wrapped the GeoLocation within a `setTimeout` so I can get an idea of how to set it up? – dKen Jan 16 '16 at 11:11
  • Yea. Added it to the answer – Chris Camaratta Jan 16 '16 at 16:46
1

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

MeMTn
  • 93
  • 1
  • 13
  • I'm not getting the continuous asking for permission at that website. What happens when you go to http://html5demos.com/geo ? – Gregory Bolkenstijn Dec 06 '14 at 17:09
  • I am getting the error on html5demos.com/geo but not here http://www.w3schools.com/html/html5_geolocation.asp. Weird... – stianlp Feb 26 '15 at 14:19
1

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.

stianlp
  • 999
  • 9
  • 19
  • Yea, this feels like a bug in Safari. Reiterating my earlier point, pausing JavaScript with a breakpoint didn't stop additional popups (as one would expect if it were an issue with our code). In our case the call stack was fairly large when we asked for location; setting the timeout effectively cleared it. The w3Schools example also had a small call stack, so whatever this is, Safari's call stack handling certainly appears to be a factor. – Chris Camaratta Feb 26 '15 at 20:21