Html 单击按钮时创建允许/阻止浏览器请求

Html 单击按钮时创建允许/阻止浏览器请求,html,twitter-bootstrap,browser,Html,Twitter Bootstrap,Browser,我想实现的是,这是一个最简单的表单,当单击按钮时,它会提示用户允许/阻止浏览器的位置请求 这不需要运行,也不需要实际获取访问者的位置,只要在请求敏感用户信息(如他们的位置)时证明最佳做法即可。您可以通过简单使用html和Javascript来实现这一点。您可以使用getCurrentPosition函数。完整的例子见此 以下是如何实现此目标的示例代码: <html> <body> <p>Click the button to get your coordin

我想实现的是,这是一个最简单的表单,当单击按钮时,它会提示用户允许/阻止浏览器的位置请求


这不需要运行,也不需要实际获取访问者的位置,只要在请求敏感用户信息(如他们的位置)时证明最佳做法即可。

您可以通过简单使用html和Javascript来实现这一点。您可以使用getCurrentPosition函数。完整的例子见此

以下是如何实现此目标的示例代码:

<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>

这专门用于获取您的位置。

您想知道如何触发浏览器自己的提示吗?每当页面试图请求位置时,浏览器就会发出提示。提示不是html的一部分,而是浏览器本身的一部分。