您的位置:首页 > Web前端 > JavaScript

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

2013-04-08 10:19 351 查看
判断子页面的域是否与父页面的域相同:

document.domain == parent.document.domain

关于iframe的自适应,跨域可以参考下面步骤:

1.在父窗口加上document.domain(也就是调用IFRAME的那个窗口)

//如“www.a.com”中的"a.com"
document.domain = "a.com";


2.在子窗口加上下边代码(也就是IFRAME本身里边)

document.domain = "a.com";

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);
}


3.细节问题 IFRAME外边加个DIV,否则火狐下有问题,宽度设置好 否则它们都有问题

<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>

OK 了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: