gpt4 book ai didi

javascript - 为什么我的 test_loc.php 会自行重新加载

转载 作者:行者123 更新时间:2023-12-01 01:35:16 25 4
gpt4 key购买 nike

我尝试获取地理位置坐标并在页面加载时将其发送到url

我在 JavaScript 中使用了 onload 函数。当我运行 js 时,它可以工作,但它会反复刷新自己。

<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.";
}
}
window.onload = getLocation;

function showPosition(position) {
window.location.href = "test_loc.php?latitude=" + position.coords.latitude +
"&longitude=" + position.coords.longitude;

}
</script>

最佳答案

重新加载由以下行触发,该行附加了异步调用的纬度经度:

window.location.href = "test_loc.php?latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;

你可以做的是,你可以检查参数是否存在并且可以停止回调。请看这个:How to get the value from the GET parameters?

根据其他问题的答案,您可以实现如下内容:

function showPosition(position) {
if (typeof parse_query_string("latitude") == "undefined")
window.location.href = "test_loc.php?latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;
}

关于javascript - 为什么我的 test_loc.php 会自行重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52890945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com