Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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 iframe中的动态变量_Javascript_Html_Iframe_Geolocation - Fatal编程技术网

Javascript iframe中的动态变量

Javascript iframe中的动态变量,javascript,html,iframe,geolocation,Javascript,Html,Iframe,Geolocation,我试图使用HTML5地理编码将动态lat和long传递到iframe中。这是我能做到的。我不能完全让lat和lon进入iframe src 如何将变量正确地传递到iframe中 <!DOCTYPE html> <html> <body> <script type="text/javascript"> function myLat(position) { text(position.coords.latitude); } functio

我试图使用HTML5地理编码将动态lat和long传递到iframe中。这是我能做到的。我不能完全让lat和lon进入iframe src

如何将变量正确地传递到iframe中

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

function myLat(position) {
    text(position.coords.latitude);
}

function myLon(position) {
    text(position.coords.longitude);
}

document.write('<iframe id="forecast_embed" type="text/html" frameborder="0" height="245" width="100%" src="http://forecast.io/embed/#lat='+myLat+'&lon='+myLon+'&name=Your Location"> </iframe>');

</script>

</body>
</html>

功能myLat(位置){
文本(位置坐标纬度);
}
功能myLon(位置){
文本(位置坐标经度);
}
文件。写(“”);

您应该首先定义iframe,然后从JavaScript设置其源代码:

<iframe id="forecast_embed" type="text/html" frameborder="0" height="245" width="100%" src=""> </iframe>


<script>
function myLat(position) {
    text(position.coords.latitude);
}

function myLon(position) {
    text(position.coords.longitude);
}

document.getElementById("forecast_embed").src = "http://forecast.io/embed/#lat="+myLat+"&lon= "+myLon+"&name=Your Location";
</script>

功能myLat(位置){
文本(位置坐标纬度);
}
功能myLon(位置){
文本(位置坐标经度);
}
document.getElementById(“forecast_embed”).src=”http://forecast.io/embed/#lat=“+myLat+”&lon=“+myLon+”&name=您的位置”;

您有没有看过有关的文档?类似于以下内容的操作将满足您的要求:

navigator.geolocation.getCurrentPosition(function(position) {

    var lat = position.coords.latitude;
    var lon = position.coords.longitude;

    document.write('<iframe id="forecast_embed" type="text/html" frameborder="0" height="245" width="100%"' +
        'src="http://forecast.io/embed/#lat=' + lat + '&lon=' + lon + '&name=Your Location"> </iframe>');
});
navigator.geolocation.getCurrentPosition(函数(位置){
var lat=位置坐标纬度;
var lon=位置坐标经度;
文件。写(“”);
});