2012-07-27 53 views
0

這是最新的寫Javascript變量到<a href> - 不onclick

任何人都知道爲什麼這不會重定向?

function onGeoSuccess(event) 
{ 
    document.getElementById("Latitude").value = event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude; 
    document.getElementById("location").href = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude; 

var redirectUrl = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude; 

} 

function redirect() 
{ 
    window.location.href = redirectUrl; 
} 
setTimeout(redirect,15000); 

我卡在腦屁模式,似乎無法寫入一個JavaScript變量的錨點href。想法?我敢肯定,我失去了一些東西簡單...

下方,以便JavaScript是拉動全球定位系統 - 正常工作...

我需要它來填充緯度和長成一個錨的href ......沒有一個「onclick」事件 - 需要自動填充......

所以:

<a href="page.htm?Lat=javascript(latitude)&Long=javascript(longitude)">GPS Location</a> 

javascript(latitude) - is variable I need to pull from below Javascript 
javascript(longitude) - is variable I need to pull from below Javascript 

    <script type="text/javascript"> 
    function getLocationConstant() 
{ 
    if(navigator.geolocation) 
    { 
     navigator.geolocation.watchPosition(onGeoSuccess,onGeoError); 
    } else { 
     alert("Your browser or device doesn't support Geolocation"); 
    } 
} 

// If we have a successful location update 
function onGeoSuccess(event) 
{ 
    document.getElementById("Latitude").value = event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude; 

} 

// If something has gone wrong with the geolocation request 
function onGeoError(event) 
{ 
    alert("Error code " + event.code + ". " + event.message); 
} 

    </script> 
+0

得到它通過消除VAR想通重定向 – 2012-07-28 15:48:18

回答

3
<a href="" id="location">GPS Location</a> 

<script> 
function onGeoSuccess(event) 
{ 
    document.getElementById("Latitude").value = event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude; 

    document.getElementById("location").href = "page.htm?Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude; 
} 
</script> 
+0

之前似乎沒有加載正確的網址 - 只加載到page.htm - 沒有問號或座標後... – 2012-07-27 20:21:50

+0

它是否在頁面上使用正確的值填充佔位符? – 2012-07-27 20:23:11

+0

正確結束href引號是...並正確顯示鏈接GPS位置...沒有座標數據 - 我可以得到onclick拉入與其他編碼罰款... – 2012-07-27 20:35:49

0

佐爾坦讓我在正確的方向開始 - 不能把它太拉,不得不添加一個onload在身體。

現在我想重定向到URL,它不會與JavaScript重定向去...

THX佐爾坦

<script type="text/javascript"> 

// Get a single location update 
function getLocationConstant() 
{ 
    if(navigator.geolocation) 
    { 
     navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError,{enableHighAccuracy:true,maximumAge:0}); 
    } else { 
     alert("Your browser or device doesn't support Geolocation"); 
    } 
} 

// If we have a successful location update 
function onGeoSuccess(event) 
{ 
    document.getElementById("Latitude").value = event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude; 
    document.getElementById("location").href = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude; 

var redirectUrl = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude; 

} 

// If something has gone wrong with the geolocation request 
function onGeoError(event) 
{ 
    alert("Error code " + event.code + ". " + event.message); 
} 

function redirect() 
{ 
    window.location = redirectUrl; 
} 
setTimeout(redirect,15000); 
    </script> 


    </head> 

    <body onload="getLocationConstant()"> 
    <br><br> 
    <a href="" id="location">URL?</a>