`

iframe 跨站自适应高度

 
阅读更多
要在a.com/a.html 中 包含 b.com/b.html 操作步骤
1:a.html
<iframe id="commentiframe" src="http://b.com/b.html" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" ></iframe>

2: b.html
除了内容外加一段
<iframe id="c_iframe"  height="0" width="0"  src="" ></iframe>
<script type="text/javascript">
    (function autoHeight(){
        var b_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
        var b_height = Math.max(document.body.scrollHeight,document.body.clientHeight);
        var c_iframe = document.getElementById("c_iframe");
        c_iframe.src='http://a.com/temp.html#'+b_height;
    })();
</script>

3:在a.com 目录增加一个temp.html 文件,代码为
<script type="text/javascript">
    var b_iframe = window.parent.parent.document.getElementById("commentiframe");
    var hash_url = window.location.hash;
    if(hash_url.indexOf("#")>=0){
        var hash_height = hash_url.split("#")[1]+"px";
        //var hash_width = hash_url.split("#")[1].split("|")[1]+"px";
        //b_iframe.style.width = hash_width;
        b_iframe.style.height = hash_height;
    }
</script>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics