将Javascript变量写入<a href=""> - without onclick</a>

时间:2012-07-27 19:59:14

标签: javascript gps

这是最近的

任何人都知道为什么这不会重定向?

 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变量写入A锚点href。想法?我确定我错过了一些简单的事情......

所以Javascript在下面拉GPS - 工作正常......

我需要它将Lat和Long填充到一个锚点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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

Zoltan让我开始朝着正确的方向前进 - 无法让它拉得太大,不得不在身体上添加一个载荷。

现在我正在尝试重定向到该网址,它不会使用javascript重定向...

Thx Zoltan

  <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>