一、完全跨域(域名完全不同,不兼容IE6)
某次需求需要在页面中签入iframe,iframe的src页面和主页面不在同一个域内,而且iframe的内容也不是固定的,所以宽度需要自适应设置,经过网上的搜索探索,终于整好了,分享下代码;
1 www.a.com/index.html页面内容
<iframe src="http://www.b.com/b.html" name="aIframeid" id="aIframeid" frameborder="no" border="0px" marginwidth="0"
marginheight="0" scrolling="no" allowtransparency="yes" ></iframe>
2 www.b.com/b.html页面内容
<iframe id="iframeProxy" name="iframeProxy" height="0" width="0" src="" style="display:none" ></iframe>
{literal}
<script type="text/javascript">
function sethash(){
hashH = document.documentElement.scrollHeight; //获取自身高度
urlC = "http://www.a.com/a_proxy.html"; //设置iframeA的src
document.getElementById("iframeProxy").src=urlC+"#"+hashH; //将高度作为参数传递
}
window.οnlοad=sethash;
</script>
3 www.a.com/a_proxy.html 中介页面
<script>
function pseth() {
var iObj = parent.parent.document.getElementById('aIframeid');//A和main同域,所以可以访问节点
iObjH = parent.parent.frames["aIframeid"].frames["iframeProxy"].location.hash;//访问自己的location对象获取hash值
iObj.style.height = iObjH.split("#")[1]+"px";//操作dom
}
pseth();
</script>
二 、 二级域名相同,三级域名不同
上面的方法也可以,但是也可以用一种最简单的方法:
在子页面中获取到本身的高度,然后写到cookie中,cookie的path设置为***.com,然后在父页面中获取这个cookie,
当然,在页面每次加载的时候,将这个cookie值设为空,然后再赋值本身的高度。只要子页面加载就重新赋值。