Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 geolocation not working on safari browser

working on html5 geolocation.

tried on mozilla, chrome, IE. working fine on them but not working for safari. tested on safari(8.0.5) on mac, safari(5.1) on windows

simply hitting the url

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

OR

http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

  <!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
}
</script>

</body>
</html>

it gives popup Allow/Disallow but does nothing after allowing it. And i have tested for safari on Mac connected with wi-fi But not for laptop on wi-fi and LAN too.

like image 451
Kamini Avatar asked Dec 18 '22 23:12

Kamini


2 Answers

Are you connected with wi-fi or over cable network?

It seems safari has trouble when you aren't connected with wi-fi.

Source

Geolocation not workin on Safari 5.x on Windows 7/XP

Geolocation in Safari 5

like image 79
Paran0a Avatar answered Dec 21 '22 12:12

Paran0a


Based on the example in the question, the following is a complete working snippet:

<p id=demo>
<script>
var x = document.getElementById("demo");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
    "<br>Longitude: " + position.coords.longitude;
}
getLocation()
</script>

The actual example in the question is incomplete, so it would never do anything as-is by itself. So it’s hard to know what might have been working in Mozilla, Chrome, and IE without seeing the complete code.

If the above snippet still doesn’t work for you in your Safari, open Safari’s Web Inspector (for example, from the Develop menu or by right-clicking on the page and selecting Inspect Element), and go to the Console tab there and check to see what errors have been logged.

For now, as far as the code snippet, some basic points to note:

  • the script must actually call the getLocation() function (not just define it)
  • the document must actually have an id=demo element
  • the script should be placed after the id=demo element (if you put the call to getLocation() in, for example, the head of the document, it’s not going to work, because it will run before the id=demo element exists in the DOM)

And never use w3schools.com for anything. It’s sloppy and highly unreliable and very poorly maintained. Instead use MDN. For example, MDN has a very good Using geolocation page, with complete code for a good live example.

like image 27
2 revs Avatar answered Dec 21 '22 11:12

2 revs