繁体   English   中英

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

[英]Click to redirect user based on geolocation

我想根据用户的位置单击按钮,将用户重定向到特定的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语句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM