利用location.hash实现跨域iframe自适应 |
本文标签:iframe,自适应 页面域关系: 复制代码 代码如下: <iframe id=”b_iframe” height=”0″ width=”0″ src=”http://www.baidu.com/b.html” frameborder=”no” border=”0px” marginwidth=”0″ marginheight=”0″ scrolling=”no” allowtransparency=”yes” ></iframe> b.html代码 复制代码 代码如下: <script type=”text/javascript”> var b_width = Math.max(document.documentElement.clientWidth,document.body.clientWidth); var b_height = Math.max(document.documentElement.clientHeight,document.body.clientHeight); var c_iframe = document.getElementById(”c_iframe”); c_iframe.src = c_iframe.src+”#”+b_width+”|”+b_height; //http://www.jb51.net/c.html#width|height” } </script> <!–js读取b.html的宽和高,把读取到的宽和高设置到和a.html在同一个域的中间代理页面车c.html的src的hash里面–> <iframe id=”c_iframe” height=”0″ width=”0″ src=”http://www.jb51.net/c.html” style=”display:none” ></iframe> c.html代码 复制代码 代码如下: <script type=”text/javascript”> var b_iframe = parent.parent.document.getElementById(”b_iframe”); var hash_url = window.location.hash; var hash_width = hash_url.split(”#”)[1].split(”|”)[0]+”px”; var hash_height = hash_url.split(”#”)[1].split(”|”)[1]+”px”; b_iframe.style.width = hash_width; b_iframe.style.height = hash_height; </script> a.html中的iframe就可以自适应为b.html的宽和高了. 其他一些类似js跨域操作问题也可以按这个思路去解决 如果不是跨域访问,可以参考这篇文章 。iframe自适应高度的多种方法方法小结 |