2012-07-27 34 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>