Javascript 为什么我的test_loc.php会自己重新加载
我尝试获取地理位置坐标,并在加载页面时将其发送到Javascript 为什么我的test_loc.php会自己重新加载,javascript,php,google-maps,geolocation,location,Javascript,Php,Google Maps,Geolocation,Location,我尝试获取地理位置坐标,并在加载页面时将其发送到url 我在javascript中使用了onload函数。当我运行js时,它可以工作,但它会反复刷新自己 <script> var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosi
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>
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
window.onload=getLocation;
功能显示位置(位置){
window.location.href=“test_loc.php?latitude=“+position.coords.latitude”+
“&longitude=“+position.coords.longitude;
}
重新加载由以下行触发,该行附加了异步调用的纬度
和经度
:
window.location.href = "test_loc.php?latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;
您可以做的是,检查参数是否存在,然后停止回调。请看这个:
根据另一个问题的答案,您可以执行以下操作:
function showPosition(position) {
if (typeof parse_query_string("latitude") == "undefined")
window.location.href = "test_loc.php?latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;
}
重新加载由以下行触发,该行附加异步调用的
纬度
和经度
:
window.location.href = "test_loc.php?latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;
您可以做的是,检查参数是否存在,然后停止回调。请看这个:
根据另一个问题的答案,您可以执行以下操作:
function showPosition(position) {
if (typeof parse_query_string("latitude") == "undefined")
window.location.href = "test_loc.php?latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude;
}
因为您将
showPosition
作为成功回调传递给geolocation.getCurrentPosition
,然后设置重新加载页面的location.href
。因为您将showPosition
作为成功回调传递给geolocation.getCurrentPosition
,然后设置location.href
重新加载页面。每次加载页面时,您都通过“onload”事件调用getLocation(),该事件依次调用getCurrentPosition(),然后使用window.location.href重新加载页面。然后,当页面重新加载时,onload事件再次触发,整个过程从头开始
您基本上创建了一个无限循环
为了传递坐标,真的需要刷新页面吗?如果需要将它们发送到服务器,请考虑使用Ajax代替.< /P> < P>每次加载页面时,通过“OnLoad”事件调用GETLoopStage(),然后调用GeCurrnPosithOffice(),然后用Wr.Office .HREF重新加载页面。然后,当页面重新加载时,onload事件再次触发,整个过程从头开始 您基本上创建了一个无限循环 为了传递坐标,真的需要刷新页面吗?如果需要将它们发送到服务器,请考虑使用Ajax代替。