单击以根据地理位置重定向用户



我想在单击按钮时将用户重定向到特定的HTML文件。 具体来说 - 我想在单击按钮时获取它们的位置,并将它们重定向到正确的 HTML 文件。

我从w3c学校得到了这个代码,但我不知道下一步该怎么做。

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(pos) {
//You have your locaton here
console.log("Latitude: " + pos.coords.latitude +
"Longitude: " + pos.coords.longitude);
});
} else {
console.log("Geolocation is not supported by this browser.");
}
}
<button onclick="getLocation()">Get Location</button>

您可以使用:

window.location.replace("http://example.com");

或:

window.location.href = "http://example.com";

在 javascript 函数中获取位置(纬度和经度(后

给你的按钮一个id,然后在你的脚本中做类似的事情。

<script type="text/javascript">
document.getElementById("myButton").onclick = function () {
location.href = "www.site123.com";
};
</script>

并根据经纬度变量在函数中添加 if 语句

最新更新