如何将数据从父页面传递到弹出窗口?

时间:2014-10-28 06:55:02

标签: javascript google-maps data-transfer

我有以下代码:

父页面:

<!DOCTYPE html>

    <html>
      <head>
      </head>
      <body>
      <a href=# onclick='showMap()'>show map <a>
        <div id="map-canvas"></div>
      </body>
       <script>
       function showMap(){

         window.open('map.html','map','width=600,height=400');
        }
         </script>
    </html>

map.html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info windows</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>


function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)'
  });

}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

如您所见,我使用硬编码值进行地图标记渲染:

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);

我可以将这些值从父页面传递给弹出窗口吗?

2 个答案:

答案 0 :(得分:2)

由于您询问如何从父级传递到弹出窗口,因此选项将是查询字符串参数:

<a href=# onclick='showMap(-25.363882,131.044922)'>show map <a>

function showMap(lat,lng){
 window.open('map.html?lat='+lat+'&lng ='+lng,'map','width=600,height=400');
}

然后在map.html中收集这些值并使用..

更新:收集查询字符串值可以通过多种方式完成,通过快速搜索查看选项

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
       var lat = getParameterByName('lat');
       var lon = getParameterByName('lon');

取自:https://stackoverflow.com/a/901144/306921

您还可以查看:Get query string parameters with jQuery

我测试了它的全部工作:

结果index.html:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
  <a href=# onclick='showMap(-25.363882,131.044922)'>show map <a>
    <div id="map-canvas"></div>
  </body>
  <script>
   function showMap(lat,lng){
     window.open('map.html?lat='+lat+'&lng ='+lng,'map','width=600,height=400');
    }
  </script>
</html>

以及生成的map.html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info windows</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
           var lat = getParameterByName('lat');
           var lon = getParameterByName('lon');

    function initialize() {
      var myLatlng = new google.maps.LatLng(lat, lon);
      var mapOptions = {
        zoom: 4,
        center: myLatlng
      };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)'
  });

}

google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

答案 1 :(得分:1)

QueryString是一个很好的方法。但是,根据您的要求,您还可以将其存储在父页面中并通过javascript阅读。

使用dom:

在父页面中存储值
<input type="hidden" id="lat" value="...."/>

您还可以谷歌查找如何使用窗口对象本身存储数据(例如window.myVar =&#39; xxx&#39;)并从弹出窗口中读取它。

通过window.open

访问弹出页面中的值
var lat =window.opener.getElementById("lat").value;//use window.parent if opened as iframe