Auto height iframe javascript

1

I have a problem, I am creating by JavaScript a iframe

var iframe = document.createElement("iframe");
  iframe.src = 'web.html';
  iframe.scrolling = 'no';
  iframe.style.width = options.width;
  iframe.style.border = 'none';
  iframe.style.overflow = 'hidden';
  iframe.id = 'ol-widget';
  element.appendChild(iframe);

I want to get Height of web.html and put it in iframe

    
asked by Jadel 31.07.2017 в 08:23
source

1 answer

1

As the height of iframe does not exist until the content has been loaded (html and css) you must first let it load the content and then update the size of the iframe.

For this you can put a listener to the "load" event so that I executed a function:

iframe.addEventListener('load', actualizar_altura);

Then we define the function actualizar_altura() in the following way:

function actualizar_altura() {
    var iframe = document.getElementById('ol-widget');
    iframe.style.height = iframe.contentWindow.document.body.offsetHeight+'px';
}

That would be all, tell me if it worked for you, I hope you solve your problem.

    
answered by 31.07.2017 в 09:11