簡體   English   中英

JavaScript不斷刷新頁面

[英]Javascript keeps reloading page

我有以下重新加載頁面的腳本,因此可以獲取經度和緯度變量。 我希望它重新加載一次,但是,頁面會不斷刷新。 我似乎不知道為什么。 任何見解將非常有幫助。

請在下面找到我的代碼:

window.onload = function getLocation () {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition (position) {
    window.location='location.php?  lat='+position.coords.latitude+'&long='+position.coords.longitude;
}

您無需檢查latlong參數是否設置,因此可以不斷刷新。 為了避免這種情況,您可以檢查以下服務器端:

<?php
    if(isset($_GET['lat']) && isset($_GET['long'])){
?>
    <h1>I know where you are!</h1>
<?php
    } else {
?>
    <script>
        /* Your JS script */
    </script>
<?php
    }
?>

聽起來好像您不想重新加載頁面(如果參數已經存在)。 為此,將if子句添加到showPosition函數中:

function showPosition(position) 
{
  if(locationSet()) {
    window.location='location.php?  lat='+position.coords.latitude+'&long='+position.coords.longitude;
  }
}
function locationSet() {
  // TODO write function that parses window.location.search
  // for lat & long params & returns true if both set
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM