两个页面:
a.htm为top页面,b.htm在iframe中.
域都为 api.adomain.com
注:所有文字说明都在注释中,只需看文字即可.
a.htm代码:
b.htm代码:
by: 天堂左我往右
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: 天堂左我往右