Ricky T
Ricky T

Reputation: 243

How can I pass a Javascript ID value into window.location.href

The Latitude and longitude are displaying so I know the script is working up to the point of getting getting lat and lon. My problem is passing the id value in the window.location.href. I get [object%20HTMLSpanElement] as my value. here is the URL it takes me to "dummy website" https://https://my_website/index.php?category=fast%20food&gps_geo_latitude=[object%20HTMLSpanElement]&gps_geo_longitude=[object%20HTMLSpanElement]. The point is to notice the values for geo_latitude and geo_longitude. I have tried to add .value to variable like: var geo_longitude = document.getElementById('gps_geo_longitude').value; and when I do that the window.location.href does not appear to execute at all, and my span ID's do not display either. Thanks for any help

<body onload="getLocation()">

<span id="gps_geo_longitude"></span>
<span id="gps_geo_latitude"></span>

<script>
var geo_longitude = document.getElementById('gps_geo_longitude');
var geo_latitude = document.getElementById('gps_geo_latitude');

function getLocation() {
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition, showError);
} else { 
  geo_longitude.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {

geo_latitude.innerHTML = position.coords.latitude;

geo_longitude.innerHTML = position.coords.longitude;

window.location.href = "https://my_website/index.php?category=fast%20food&gps_geo_latitude="  + gps_geo_latitude + "&gps_geo_longitude=" + gps_geo_longitude; 

}

function showError(error) {
switch(error.code) {
  case error.PERMISSION_DENIED:
  geo_latitude.innerHTML = "User denied the request for Geolocation."
    break;
  case error.POSITION_UNAVAILABLE:
  geo_latitude.innerHTML = "Location information is unavailable."
    break;
  case error.TIMEOUT:
  geo_latitude.innerHTML = "The request to get user location timed out."
    break;
  case error.UNKNOWN_ERROR:
  geo_latitude.innerHTML = "An unknown error occurred."
    break;
}
}


 </script>
 </body>

Upvotes: 0

Views: 1639

Answers (1)

Walid Ajaj
Walid Ajaj

Reputation: 578

in showPosition() function you already have lat and long passed as position and you can use it directly like this

function showPosition(position) {
    geo_latitude.innerHTML = position.coords.latitude;
    geo_longitude.innerHTML = position.coords.longitude;

    window.location.href = "https://my_website/index.php?category=fast%20food&gps_geo_latitude="  + position.coords.latitude + "&gps_geo_longitude=" + position.coords.longitude; 
}

Or, you can get them from DOM the same way you set them in your span

function showPosition(position) {
    geo_latitude.innerHTML = position.coords.latitude;
    geo_longitude.innerHTML = position.coords.longitude;

    window.location.href = "https://my_website/index.php?category=fast%20food&gps_geo_latitude="  + geo_latitude.innerHTML + "&gps_geo_longitude=" + geo_longitude.innerHTML; 
}

Upvotes: 1

Related Questions