0

I have a leaflet map, exported from QGIS. I don't want to modify the code inside that exported folder.

I successfully embedded the map inside another page, using a <iframe> tag.

Now, I want, in the javascript code of my parent page, get the map variable so I cant tweak its parameters (position, bounds, etc...)

How can I get this map variable in my parent page javascript ?

parent

 <iframe id = 'iframeid'
   src="/qgis/index.html"
   scrolling="no"
   frameborder="0"
   style="height: 100vh;
   width: 100%">
 </iframe>
 <script>
 var map = ????
 </script>

child (qgis-exported leaflet map)

<script>
  var map = L.map('map', {zoomControl:true, maxZoom:20, minZoom:15});

  /* I need te following lines inside the parent file. */
  var lat = -0.017;
  var lng = 0.015;
  var zoom = 16;
  map.setView([lat, lng], zoom);
</script>
Jean
  • 1,707
  • 3
  • 24
  • 43
  • Possible duplicate of [How can I access iframe elements with Javascript?](http://stackoverflow.com/questions/1452871/how-can-i-access-iframe-elements-with-javascript) – I wrestled a bear once. Dec 30 '15 at 15:28
  • The map is a js variable, can the question you linked solve also when working with a js variable ? – Jean Dec 30 '15 at 15:43
  • javascript as opposed to what? js is the only scripting language you can use in a browser, so yea. everything is javascript. there are no other options. – I wrestled a bear once. Dec 30 '15 at 15:44
  • I meant that I was not sure that the method for getting an element with its id works with js variables. All those examples are for getting an element it seems. – Jean Dec 30 '15 at 20:39

1 Answers1

0

If map is the global variable referencing the Leaflet map in your iframe, then in the parent page, do

var map = document.getElementsByTagName("iframe")[0].contentWindow.map // supposing you only have one iframe, else select it by its id or something like this

to access it.

Note that it will only work if both iframe and parent share same domain, protocol and host.

Stranded Kid
  • 1,395
  • 3
  • 15
  • 23