web-dev-qa-db-ja.com

iFrameがサイズ変更イベントをトリガーしない

3日間の調査と試行錯誤の後で、iframeもそのコンテンツもサイズ変更イベントをトリガーすることができず、サイズ変更関数が呼び出されます。 ... trigger( "resize");を使用すると、手動でサイズ変更イベントをトリガーするために、私のサイズ変更関数が呼び出されて機能します。 iframeに読み込まれるページは同じドメイン(http://localhost:81/curlExample/)iframeを含むページとして。最終的に、iframe内のページはphp curlメソッドによって提供されますが、最初に機能させたいと思います。

*******更新******** iframeのサイズを調整させるブラウザーウィンドウのサイズを変更するときに、サイズ変更イベントをトリガーするにはどうすればよいですか?


ご協力ありがとうございました!

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>

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>
13
ermSO

<iframe>要素は、<img><div>などのサイズ変更イベントをトリガーしません。このイベントはwindowから取得する必要があります。 iframeドキュメントは親ドキュメントと同じOriginからのものであるため、contentWindowおよびその他の属性にアクセスできます。

だから、これを試してください:

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

私はDOMの addEventListener のみを使用してそれを行いました。

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