IFrame跨域自适应

IFrame跨域调用高度、宽度自适应,以下的解决方法原理很简单,按照步骤作,自然就明白了。

1.  被调用页面需要增加的内容(被调用页面URL:http://SiteB/B.aspx)。

1.1    增加 <iframe id="proxyIframe" height="0" width="0" style="display:none" />

1.2    增加

$(function() {

        var width = document.documentElement.clientWidth;

        var height = document.body.scrollHeight;

        if($.browser.mozilla)

        {

           height = document.documentElement.scrollHeight;

        }

        $("#proxyIframe").attr("src", "http://SiteA/Proxy.html#" + width + "|" + height);

        })

2. 调用页面需要增加的内容(调用页面所在站点:http://SiteA)

2.1 在调用页面所在站点增加Proxy.html,

脚本内容如下(无其他内容,URL: http://SiteA/Proxy.html):

<script type="text/javascript">

    var b_iframe = parent.parent.document.getElementById("Iframe1");

    var hash_url = window.location.hash;  

    var hash_height = hash_url.split("#")[1].split("|")[1]+"px"; 

    b_iframe.style.height = hash_height;

</script>

2.2 调用页面增加内容:

<iframe id=" Iframe1" src="http://SiteB/B.aspx" height="850" width="930" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" />

完成以上步骤即可。
posted @ 2011-05-03 16:11  老贾  阅读(641)  评论(3编辑  收藏  举报