javascript-在iframe中加载HTML5地理位置时访问用户的纬度和经度

我正在使用HTML5地理位置来获取用户的纬度和经度.当直接打开页面时,它可以在所有浏览器上正常工作,但是现在我必须将地理位置代码放入iframe中.将其放入iframe后,地理位置不会提示用户输入坐标.

在iframe中加载网页时,如何获取用户的纬度和经度?

假设这是代码:

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
</script>
</body>
</html>

解决方法:

您可能已经解决了这个问题,但是问题出在这里:

var x = document.getElementById(“demo”);

当您按原样运行页面时,上面的方法很好,但是通过iframe加载时,“文档”是指父框架(而不是您关心的子框架).尝试这样的事情:

var x = document.getElementById(“frameId”).contentWindow.document.getElementById(“demo”);

其中,“ frameId”是指包含显示经纬度信息的页面的框架.

使用上有一些限制,有关更多信息,请参见此出色的线程:How to pick element from inside iframe

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。