ermSO ermSO - 5 months ago 28
Javascript Question

Iframe does not trigger resize event

After 3 days of research and trial and error, I am not able to get the iframe nor its contents to trigger a resize event so that my resize function is called. If I use ...trigger("resize"); to manually trigger a resize event, my resize function is called and works. The page that is loaded in the iframe is on the same domain (

) as the page containing the iframe. Eventually, the page in the iframe will be a supplied by the php curl method, but I would like to get it working first.

How do I get the resize event triggered when I resize the browser window which causes the iframe to adjust its size?

Thank you for your help!

Page with the iframe

<!DOCTYPE html>
<script src=""></script>

function setResize()

var iframeRef = document.getElementById('displayframe');
$(iframeRef).on("resize", function(){
var xExp = 0;
window.alert("Resize event fired.");


alert("Hello from iFrame. Load event fired.");
var myStallTime = setTimeout(setResize, 3000);



<p id="myP">Hello</p>

<iframe id="displayframe" src="http://localhost:81/curlExample/HelloIframe.xhtml" style="height:250px; width:100%;">
<p>Your browser does not support iframes.</p>


Page within the iframe (HelloIframe.xhtml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="">
<title>TODO supply a title</title>
<div id="myContent" style="width:100%; height:200px;">
<h1>TODO write content</h1>
<h2>Extremity sweetness difficult behaviour he of</h2>

<p>Agreed joy vanity regret met may ladies oppose who. Mile fail as left as hard eyes. Meet made call in mean four year it to. Prospect so branched wondered sensible of up. For gay consisted resolving pronounce sportsman saw discovery not. Northward or household as conveying we earnestly believing. No in up contrasted discretion inhabiting excellence. Entreaties we collecting unpleasant at everything conviction.</p>

<p>Yet remarkably appearance get him his projection. Diverted endeavor bed peculiar men the not desirous. Acuteness abilities ask can offending furnished fulfilled sex. Warrant fifteen exposed ye at mistake. Blush since so in noisy still built up an again. As young ye hopes no he place means. Partiality diminution gay yet entreaties admiration. In mr it he mention perhaps attempt pointed suppose. Unknown ye chamber of warrant of norland arrived.</p>



The <iframe> element will never trigger a resize event, like an <img> or a <div>. You must get this event from window. As your iframed document comes from the same origin of the parent document, you can acces its contentWindow and any other attribue.

So, try this:

var iframeWin = document.getElementById('displayframe').contentWindow;
$(iframeWin).on('resize', function(){ ... });

I did it useing only DOM's addEventListener.

var iframeWin = document.getElementById('displayframe').contentWindow;
iframeWin.addEventListener('resize', function(){ ... });