如何在多个域上使用iFrameResizer?

时间:2017-03-20 19:42:44

标签: javascript jquery iframe iframe-resizer

我正在使用Iframe Resizer而我的代码不能跨域工作。当两个域相同时,它工作正常。你能帮我吗?这很奇怪,因为iframe肯定加载了iFrameResizer.contentWindow.js并且我使用checkOrigin:false所以它应该允许跨域...

以下是我在加载具有iframe的父页面时在控制台中遇到的错误:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://jacobjohnson.net') does not match the recipient window's origin ('http://designtesting.cfs.local').

iframeResizer.min.js:8 [iFrameSizer][Host page: display_frame] No response from iFrame. Check iFrameResizer.contentWindow.js has been loaded in iFrame

<div class="panel panel-default" id="iframePanel">
    <div class="panel-body" style="padding:0px;">
       <iframe id="display_frame" name="display_frame" class="frame" src="http://jacobjohnson.net/iframetest.html" allowfullscreen></iframe>
    </div>
</div>


<script>

    jQuery('#display_frame').iFrameResize( [{log:true, checkOrigin: false}] );

    var frameHeight =  3905; // $('#display_frame').height();
    var newHeight = frameHeight / 2;
    $('#iframePanel').height(newHeight);

    $(window).resize( function() {
        var frameHeight = $('#display_frame').height();
        var newHeight = frameHeight / 2;
        $('#iframePanel').height(newHeight);
    });

</script>

2 个答案:

答案 0 :(得分:2)

也许尝试验证原点

var domains = ['http://jacobjohnson.net','http://designtesting.cfs.local'];
jQuery('#display_frame').iFrameResize( [{log:true, checkOrigin: domains}] );

答案 1 :(得分:2)

您的电话应如下所示。

jQuery('#display_frame').iFrameResize({log:true, checkOrigin: false});