2014-02-14 86 views
0

我正在嘗試使腳本根據位置重定向用戶。我編寫了這個腳本和地理定位工程,但重定向不能。爲什麼我不能重定向用戶?地理位置重定向(HTML5)

<script> 
    window.onload=function(){ 
    if(navigator.geolocation) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition,showError); 
    } 
    else 
    { 
    alert("Geolocation is not supported by this browser."); 
    } 
    } 
    function showError(error) 
     { 
     switch(error.code) 
     { 
     case error.PERMISSION_DENIED: 
      alert("User denied the request for Geolocation.") 
      break; 
     case error.POSITION_UNAVAILABLE: 
      alert("Location information is unavailable.") 
      break; 
     case error.TIMEOUT: 
      alert("The request to get user location timed out.") 
      break; 
     case error.UNKNOWN_ERROR: 
      alert("An unknown error occurred.") 
      break; 
     } 
     } 


    if (pos.coords.longitude >= 45.775534 && pos.coords.longitude <= 45.775562 && pos.coords.latitude <= 15.994809 && pos.coords.latitude >= 15.994792) 
     {window.location = 'success.html'; 
     } 
     else{ 
      window.location = 'other.html'; 
     } 
    </script> 
+2

重複您以前的問題:[HTML5地理定位重定向](http://stackoverflow.com/questions/21781759/html5-geolocation-redirect)。如果問題得到解決,則編輯它以便可以重新打開。不要再問它。 – Quentin

回答

0

在您的代碼| pos |沒有定義,你需要定義函數showPosition()

function showPosition(pos) 
    { 

    if (pos.coords.longitude >= 45.775534 && pos.coords.longitude <= 45.775562 && pos.coords.latitude <= 15.994809 && pos.coords.latitude >= 15.994792) 
     { 
      window.location = 'success.html'; 
     } 
     else{ 
      window.location = 'other.html'; 
     } 
    } 

而且我注意到,在Chrome,本地文件,在地理位置似乎並沒有工作,所以我創建了一個的jsfiddle:http://jsfiddle.net/PXp9y/和使用alert()而不是window.location。一探究竟。