iFrameResizer-readyCallback似乎从未调用过

时间:2018-09-24 16:15:50

标签: iframe iframe-resizer

我正在尝试在iFrameResizer中使用方法readyCallback(),但似乎不起作用。当我将回调添加到parent iframe对象时,它永远不会被调用:

iFrameResize({
    log : true,
    checkOrigin: false,
    minHeight : 800,
    maxHeight : 4000,
    readyCallback: function(){
        // (!) never called
        console.log('ready parent callback .. '); 
    },
    messageCallback: function(data){
        // works OK
        console.log('message callback .. '); 
        console.log(data);
    },
    initCallback: function(){
        // works OK
        window.scrollTo(0,0); 
        alert("OK initiated");
    }, 
    resizedCallback : function(info){ 
        // works OK
        console.log(info);
        scrollTo(0, info.iframe.offsetTop);
    },
    heightCalculationMethod : 'taggedElement'
}, '#iframe123');

iframe元素中的iframeResizer.contentWindow.js加载并正常运行。

我想念什么吗?谢谢。

1 个答案:

答案 0 :(得分:2)

证明方法readyCallback放在错误的位置。这是工作设置:

父页面,其中包含iframe元素:

iFrameResize({
    log : true,
    checkOrigin: false,
    minHeight : 800,
    maxHeight : 4000,
    messageCallback: function(data){
        console.log('message callback .. '); 
        console.log(data);
        // scroll to top edge of iframe element
        scrollTo(0, data.iframe.offsetTop);
    },
    initCallback: function(){
        console.log("OK initiated");
        window.scrollTo(0,0); 
    }, 
    resizedCallback : function(info){ 
        console.log(info);
        scrollTo(0, info.iframe.offsetTop);
    },
    heightCalculationMethod : 'taggedElement'
}, '#iframe123');

内部iframe 元素,其中已加载iframeResizer.contentWindow.js

// solution 1:
window.iFrameResizer = {
    readyCallback: function(){
        // scroll parent to top edge of iframe element
        window.parentIFrame.scrollToOffset(0,0);
    }
}

// solution 2:
window.iFrameResizer = {
    readyCallback: function(){
      if('parentIFrame' in window){
          parentIFrame.sendMessage('Loaded iframe ['+window.parentIFrame.getId()+'].');
      }
    }
}

// load content window at the end
<script src="/js/iframeResizer.contentWindow.js"></script>