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>
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>
Share
Improve this question
asked Apr 16, 2020 at 21:46
GroguGrogu
2,5251 gold badge22 silver badges45 bronze badges
3
- You should set an error callback as well and see what you get. See also: developer.mozilla/en-US/docs/Web/API/Geolocation/… – Brad Commented Apr 16, 2020 at 21:51
- @Brad: Hey Brad okay now I see this : code: 1 message: User denied Geolocation... Thats bizzarre because they didn't even ask in the first place? – Grogu Commented Apr 16, 2020 at 21:54
- Was the user ever asked? Alternatively, the browser is configured to reject these requests, or maybe doesn't even support them on this device in the first place. – Brad Commented Apr 16, 2020 at 22:00
2 Answers
Reset to default 4Go 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.
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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744719488a4589823.html
评论列表(0条)