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

时间:2018-12-06 12:39:22

标签: html twitter-bootstrap browser

我想实现它的最简单形式:单击按钮时提示用户允许/阻止浏览器定位请求。

See example

当请求敏感的用户信息(例如他们的位置)时,这并不需要起作用或实际上获取访问者的位置只是最佳实践的证据。

1 个答案:

答案 0 :(得分:1)

您只需使用html和Javascript即可完成此操作。您可以使用getCurrentPosition()函数。有关完整示例,请参见此link

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

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

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