设置 iframe document.domain杂谈

两个页面:

a.htm为top页面,b.htm在iframe中.
域都为 api.adomain.com

注:所有文字说明都在注释中,只需看文字即可.

a.htm代码:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文件上传</title>
<script>
//页面在 api.adomain下面


/*
* 首先申明一点,
* document.domain = "api.adomain.com";
* document.domain = "adomain.com";
* 这样的设置是可以的,反之
* document.domain = "adomain.com";
* document.domain = "api.adomain.com";
* 是不行的.非ie下这样设置会保错,ie下虽然不报错但其实没成功
*
*
*/

/*
* iframe的src没有值的情况下,除IE以外,iframe的域与父页面相同, IE父页面不能设置document.domain,只要设置就跨域...(ie真是恶心)
*/
document.domain = "adomain.com";

alert(document.domain);
function init(){

document.domain = "api.adomain.com";
var i001 = document.getElementById("ifr01");
var doc = i001.contentWindow.document;
doc.domain = "api.adomain.com";
alert(doc.domain);
}
</script>
</head>
<body onload=init()>
<iframe id="ifr01" src="b.htm"></iframe>


</body>
</html>



b.htm代码:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>�ļ��ϴ�</title>
<script>
//页面在 api.adomain.com下面

/* 如果b.htm设置为document.domain = "adomain.com"; 则在IE chrome safari下,父页面可以修改b.htm的域为 api.adomain.com(chrome和safari其实没生效) ,ff和op无法修改(无法修改貌似是比较合理的做法)

如果b.htm设置为document.domain = "api.adomain.com"; 则所有浏览器下父页面都可以将其修改为adomain.com

由此可以看出,域可以由api.adomain.com修改为adomain.com,所有浏览器都支持,而不能从adomain.com修改为api.adomain.com(部分浏览器例外,ie,chrome和safari不报错,但是修改不生效)
*/
document.domain = "api.adomain.com";
alert("iframedomain:"+document.domain);
function bcalla()
{
alert(document.domain);
}
</script>
</head>
<body>
<input type=button value="invoke" onclick="bcalla()" />
</body>
</html>


by: 天堂左我往右
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值