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

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

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

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

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

最佳答案

既然你问的只是如何从父级传递到弹出一个选项将是查询字符串参数:

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

关于javascript - 如何将数据从父页面传递到弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26602459/

相关文章:

javascript - 类型具有单独的私有(private)属性声明

Javascript父子树合并

sql-server - Microsoft Codename "Data Transfer"标识列错误

javascript - 为什么我的 JavaScript 版 Google map 看起来与公共(public) map 不同?

javascript - 使用 google api 测量 3 个标记之间的距离

postgresql - 将数据从 redshift 传输到 postgresql

C# 多客户端服务器应用程序的问题

javascript - Firebug 可以在外部 JavaScript 文件中设置断点吗?

javascript - .replace 在 AngularJs 2

android - Google map Android API v2 - 简单的 map 应用程序崩溃