Reputation: 2475
I'm trying to get Safari to prompt me for my location with a very simple code. I'm working on a remote Macbook mini from my PC running Ubuntu. Inside the Macbook environment, the code below works on Firefox and Chrome perfectly fine. But on Safari nothing happens, I don't get prompt or nothing. I wish I received any errors in the console but the function does not even fire at all. Nothing happens. Nada. Anyone has ever faced a problem like this?
If I go and try it here on W3Schools same thing happen, meaning I click on Try it and nothing happens. But it works fine in all other browsers.
code
<!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>
Upvotes: 2
Views: 7070
Reputation: 2475
I solved my problem by handling errors. Safari and ios seem to require error handling to work.
This works :
<!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, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
Upvotes: 1
Reputation: 599
Go to Finder -> Apple -> System Preferences -> Security & Privacy -> Privacy then add Safari to the whitelist. Try if that works.
Or
In Safari select Safari > Preferences. Click the Privacy icon in the Preferences window. Unselect the "Deny without prompting" option.
Upvotes: 4