简体   繁体   中英

How pass data from parent page to popup?

I have following code:

parent page:

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

As you can see I use hardcode values for map marker rendering:

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

Can I pass these values from parent page to popup?

Since you asked just how to pass from parent to pop up an option would be querystring parameters:

<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');
}

Then in map.html collect those values and use..

Update: Collecting query string values can be done in a lot of ways see the options with a quick search

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');

taken from : https://stackoverflow.com/a/901144/306921

You can also take a look at: Get query string parameters with jQuery

I tested and its all working:

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

and the resulting 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>

QueryString is a good way. However based on your requirement, You can also store it in parent page and read via javascript.

Storing value in parent page using dom:

<input type="hidden" id="lat" value="...."/>

You can also google to find how to store the data with window object itself(eg window.myVar='xxx') and reading it from pop-up.

Accessing value in pop-page opened via window.open

var lat =window.opener.getElementById("lat").value;//use window.parent if opened as iframe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM