Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Javascript ID值传递到window.location.href_Javascript_Html - Fatal编程技术网

如何将Javascript ID值传递到window.location.href

如何将Javascript ID值传递到window.location.href,javascript,html,Javascript,Html,纬度和经度都显示出来了,所以我知道脚本正在工作,直到得到纬度和经度。我的问题是在window.location.href中传递id值。我得到[object%20HtmlPanelment]作为我的值。这是我访问“虚拟网站”的URL . 重点是要注意地理纬度和地理经度的值。我尝试向变量添加.value,比如:var geo_longitude=document.getElementById('gps_geo_longitude').value;当我这样做时,window.location.href

纬度和经度都显示出来了,所以我知道脚本正在工作,直到得到纬度和经度。我的问题是在window.location.href中传递id值。我得到[object%20HtmlPanelment]作为我的值。这是我访问“虚拟网站”的URL . 重点是要注意地理纬度和地理经度的值。我尝试向变量添加.value,比如:var geo_longitude=document.getElementById('gps_geo_longitude').value;当我这样做时,window.location.href似乎根本不会执行,我的span ID也不会显示。谢谢你的帮助

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

var geo_longitude=document.getElementById('gps_geo_longitude');
var geo_latitude=document.getElementById('gps_geo_latitude');
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition,showError);
}否则{
geo_longitude.innerHTML=“此浏览器不支持地理位置。”;
}
}
功能显示位置(位置){
geo_latitude.innerHTML=position.coords.latitude;
geo_longide.innerHTML=position.coords.longide;
window.location.href=”https://my_website/index.php?category=fast%20food&gps_geo_latitude=“+gps_地理_纬度+”&gps_地理_经度=“+gps_地理_经度;
}
功能错误(错误){
开关(错误代码){
案例错误。权限被拒绝:
geo_latitude.innerHTML=“用户拒绝了地理定位请求。”
打破
案例错误。位置不可用:
geo_latitude.innerHTML=“位置信息不可用。”
打破
大小写错误。超时:
geo_latitude.innerHTML=“获取用户位置的请求超时。”
打破
案例错误。未知错误:
geo_latitude.innerHTML=“发生未知错误。”
打破
}
}
showPosition()
函数中,您已经有了lat,并且长期作为
position
传递,您可以像这样直接使用它

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; 
}
或者,您可以通过在
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; 
}

使用console.log记录geo_latitude.innerHTML和geo_longitude.innerHTML的值,甚至记录您将要放入window.location.href的字符串,怎么样?这应该给你一个线索。看起来和我今天看到的其他东西很相似。事件处理程序和脚本变量超出范围。我相信,最好不要使用类似的东西,而是在JS中设置事件处理程序。谢谢,第一个选项非常有效。问题解决了。但对于你的信息,第二个选项不起作用。但不管怎样,我很乐意去。