我正在尝试使用非常简单的代码让 Safari 提示我输入位置。我正在运行 Ubuntu 的 PC 上远程 Macbook mini 上工作。在 Macbook 环境中,下面的代码在 Firefox 和 Chrome 上运行得很好。但在 Safari 上什么也没有发生,我没有得到提示或什么也没有。我希望我在控制台中收到任何错误,但该功能根本不触发。什么都没发生。没什么。有人遇到过这样的问题吗?
如果我去这里试试 W3Schools同样的事情发生了,这意味着我点击“尝试”但没有任何反应。但它在所有其他浏览器中运行良好。
代码
<!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>
最佳答案
转到 Finder -> Apple -> 系统偏好设置 -> 安全和隐私 -> 隐私,然后将 Safari 添加到白名单。尝试一下是否有效。
或者
在 Safari 中,选择 Safari > 首选项。单击“首选项”窗口中的“隐私”图标。取消选择“拒绝而不提示”选项。
关于javascript - HTML5 地理定位不会在 Safari 浏览器上提示输入位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61260570/