Resize event only fires once

378 views Asked by At

Using the javascript onresize, only works one time. I need it to work multiple times. Here is my code:

var hh = window.outerHeight;
var ww = window.outerWidth;

document.write("<iframe src='http://www.example.com' align='left' width='" + ww + "px' height='600px' frameborder='0'></iframe>");



window.onresize = function (){

    hh = window.outerHeight;
    ww = window.outerWidth;

    document.write("<iframe src='http://www.example.com' align='left' width='"     + ww + "px' height='600px' frameborder='0'></iframe>");

}
1

There are 1 answers

1
LearnMoreCoding.com On

You are creating a new iframe every time. It is better to just select the one that has already been made.

var hh = window.outerHeight;
var ww = window.outerWidth;

document.write("<iframe src='http://www.example.com' align='left' width='" + ww + "px' height='600px' frameborder='0'></iframe>");

window.onresize = function (){

    hh = window.outerHeight;
    ww = window.outerWidth;

    document.querySelector("iframe").setAttribute('width', ww+'px');

}