Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (http://localhost:81/curlExample/) 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.

*******Updated******** 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>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   
    function setResize()
    {
        window.alert("Hello");
      
        var iframeRef = document.getElementById('displayframe');
        $(iframeRef).on("resize", function(){
            var xExp = 0;
            window.alert("Resize event fired.");
            
        });
    }
  
    $('#displayframe').load(function()
    {
        alert("Hello from iFrame.  Load event fired.");
        var myStallTime = setTimeout(setResize, 3000);

    });

});
</script>
</head>
<body>

<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>
</iframe>

</body>
</html>

Page within the iframe (HelloIframe.xhtml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
        <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>

        </div>
    </body>
</html>
like image 644
ermSO Avatar asked Jan 08 '15 17:01

ermSO


1 Answers

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

So, try this:

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

I did it using only DOM's addEventListener.

var iframeWin = document.getElementById('displayframe').contentWindow;
iframeWin.addEventListener('resize', function(){ ... });
like image 146
Aurium Avatar answered Oct 08 '22 09:10

Aurium