iframe自适应,跨域,JS的document.domain

转载:iframe自适应,跨域,JS的document.domain
http://www.cnblogs.com/olartan/archive/2009/09/23/1572264.html
1.再父窗口加上document.domain(也就是调用IFRAME的那个窗口)
XML/HTML代码
<script type="text/javascript">  
document.domain = "cnmo.com";   
</script>  

2.在子窗口加上下边代码(也就是IFRAME本身里边)
<script type="text/javascript">  
document.domain = "cnmo.com";   
</script>  
<script type="text/javascript">  
function iframeAutoFit()   
{   
     try   
     {   
         if(window!=parent)   
         {   
             var a = parent.document.getElementsByTagName("iframe");   
             for(var i=0; i<a.length; i++)   
             {   
                 if(a[i].contentWindow==window)   
                 {   
                     var h1=0, h2=0, d=document, ddd=d.documentElement;   
                     a[i].parentNode.style.height = a[i].offsetHeight +"px";   
                     a[i].style.height = "10px";   
  
                     if(dd && dd.scrollHeight) h1=dd.scrollHeight;   
                     if(d.body) h2=d.body.scrollHeight;   
                     var h=Math.max(h1, h2);   
  
                     if(document.all){h += 4;}   
                     if(window.opera){h += 1;}   
                     a[i].style.height = a[i].parentNode.style.height = h +"px";   
                 }   
             }   
         }   
     }   
     catch (ex){}   
}   
if(window.attachEvent)   
{   
     window.attachEvent("onload",   iframeAutoFit);   
}else if(window.addEventListener)   
{   
     window.addEventListener('load',   iframeAutoFit,   false);   
}   
</script>    

3 细节问题 IFRAME外边加个DIV,否则火狐下有问题,宽度设置好 否则他们都有问题
XML/HTML代码
<div style="width:100%;margin:0 0 10px 0;">   
<iframe align='middle' src='http://comments.cnmo.com/iframe_comment.php?kindid=9&articleid=<?=$pic_id?>&tw=620&style=11&font_num=180&pagesize=3' frameborder='0' marginwidth='0' marginheight='0' width='100%' scrolling='no' height='450px' style='margin-top:15px;'></iframe>   
</div>  

你可能感兴趣的:(java,html,xml)