Jump to content

Need Help Setting <a href>


davefootball123

Recommended Posts

I have a link <a href="http://www.sowx.ca/eccast/eccast.php"> as well as a javascript function that gets latitude and longitude seen below. What I need to do is take the latitude longitude and have it set the link as <a href="http://www.sowx.ca/eccast/eccast.php?lat=+lat&lon=+lon> fron the javascript function. I was able to do this by setting the href with an id and changing the . href attribute with the id however It did not work in Firefox. I imagine I have to use onclick() or something along those lines. If someone could help me out with this that would be awesome.

 

Thanks, Dave

				<script type="text/javascript">

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
  } else {
  alert('Geolocation is required for this page,');
}

function successFunction(position) {
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
  
}

function errorFunction(position) {
  alert('Error!');
}
</script>
Link to comment
https://forums.phpfreaks.com/topic/275248-need-help-setting/
Share on other sites

So what was the code you tried? I assume it went inside successFunction()?

Yes it did for example I had the code seen below and my <a href had an id of "loc", worked ok in chrome and IE...just not firefox. 

document.getElementById("loc").href='eccast.php?lat='+lat+'&lon='+lon; 
Link to comment
https://forums.phpfreaks.com/topic/275248-need-help-setting/#findComment-1416596
Share on other sites

 

window.onerror = function(message, url, lineNumber) {  
  alert(message + " | " + lineNumber + " | " + url);
  return true;
};  

 

Add the code above and / or look at your console. (Run it again afterwards.)

Thanks for your response.

Surprisingly it doesn't return an error. I notice this. It works as it should in Chrome and in Internet Explorer, however I have to load the page and then do 1 refresh for it to work in Firefox. Odd. Any thoughts?

 

Full code is posted below, minus the code you gave me above. 

 

				<script type="text/javascript">

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
  } else {
  alert('Geolocation is required for this page,');
}

function successFunction(position) {
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
  
 document.getElementById("loc").href='http://www.sowx.ca/eccast/eccast.php?lat='+lat+'&lon='+lon; 
}

function errorFunction(position) {
  alert('Error!');
}
</script>
<ul>
   <li class="current"><a href='http://www.sowx.ca'><span class="current">Warnings</span></a></li>
   <li><a href="http://www.sowx.ca/eccast/eccast.php" id="loc"><span>Local</span></a></li>
</ul>

Link to comment
https://forums.phpfreaks.com/topic/275248-need-help-setting/#findComment-1416629
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.