Iframe跨域自适应高度

Iframe跨域自适应高度:


父页面(地址是:http://www.aosachina.com/test/index.html)
   <iframe id="iframeB"  name="iframeB" src="http://aosa.eee114.com" width="100%" height="auto" scrolling="no" frameborder="0"></iframe>
若不兼容可用 <frameset rows="*" frameborder="no" border="0" framespacing="0" width="100%" height="auto">
  <frame src="http://aosa.eee114.com" id="iframeB"  name="iframeB"  />
  </frameset>
 
中间页(http://www.aosachina.com/test/current.html):
<script type="text/javascript">
        function pseth() {
            var iObj = parent.parent.document.getElementById('iframeB'); //A和main同域,所以可以访问节点
            var iObjH = parent.parent.frames["iframeB"].frames["iframeA"].location.hash; //访问自己的location对象获取hash值
            iObj.style.height = iObjH.split("#")[1] + "px"; //操作dom
        }
        pseth();
    </script>
 
被嵌套页(http://aosa.eee114.com):
<iframe id="iframeA" name="iframeA" src="" width="0" height="0" style="display:none;" ></iframe> 
<script type="text/javascript"> 
function sethash(){ 
   var hashH = document.documentElement.scrollHeight; //获取自身高度 
   var urlC = "http://www.aosachina.com/test/current.html"; //设置iframeA的src 
    document.getElementById("iframeA").src=urlC+"#"+hashH; //将高度作为参数传递 
window.onload=sethash; 
</script>
 
posted @ 2012-04-17 17:35  VictoryDou  阅读(194)  评论(0)    收藏  举报