0

First place, sorry for a newbie question. Well, I' am trying to acess a iframe element and assign the value from a div to a variable using javascript.

Originally, the html has no iframe. It is created with a click event function in Openlayers3, which creates a url and assign it to the iframe source.

To better explain, here is my code:

$(document).ready(function() {
 var map = new ol.Map(); // create some map.
 var a = [...]; // array with 'n' elements, each with a source.
 map.on('singleclick', function(evt) {
  document.getElementById('info').innerHTML = '';
  var b = [];
  for(i=0; i<n; i++){
    var url = a[i].getSource().getGetFeatureInfoUrl(
        evt.coordinate, someresolution, "EPSG:...", {'INFO_FORMAT':'text/html'}
    );
    if(url){
      document.getElementById('info').innerHTML='<iframe id="frame" seamless src="'+url+'"></iframe>';
    }
    document.getElementById('frame').onload = function(){f()};
    function f(){
     return document.getElementById('frame').contentWindow.document.getElementById('t').innerHTML;
    }
    b[i] = f();
  }
 });
});

With this code I can't store the content of the div t in the variable b.

I did some test with the alert function and actually the value of the div was printed on my screen, but it can't assing it to b. Oddly, in these test the value from the first iteration was skiped.

Could someone please help me on these issue?

groc
  • 1
  • Where is `n` defined? – guest271314 May 26 '17 at 00:10
  • It makes no sense to have a `return` statement in the `onload` callback. It runs asynchronously. `b[i] = f()` won't do anything useful, because it's running before the iframe is loaded. – Barmar May 26 '17 at 00:21

0 Answers0