标签:log eve 页面 body attach htm 动态创建 content ati
页面A:页面B,你能传个数据给我吗?
页面B:额,我们不在同一个域内,因为涉及到WEB安全问题,所以浏览器禁止我直接向你传数据。
页面A:有什么办法可以解决这个问题吗?
页面B:可以使用window.name。
页面A:那你需要我做些什么?
页面B:1.你先动态创建一个iframe节点;
2.设置节点属性src指向我;
3.我会设置window.name = ‘b‘;
4.然后你重新设置节点iframe的属性src指向和你在同一个域下的兄弟页面C;
5.虽然属性src的值变了,但是window.name的值并没有变,还是b,这样你就可以直接从页面C中获取数据b了。
页面A:明白了,这样就从跨域访问变成了同域访问。
页面B:是的。对了,这里的页面C没有实际的内容,只是起到一个桥梁的作用噢。
<!--页面A-->
<script>
var state = 0;
ifr = document.createElement(‘iframe‘);
ifr.src = ‘http://b.com/b.html‘;
if(ifr.attachEvent) {
ifr.attachEvent(‘onload‘, loadfn);
}
else
{
ifr.onload = loadfn;
}
function loadfn() {
if (state === 1) {
var data = ifr.contentWindow.name;
alert(data);
}
else if (state === 0)
{
state = 1;
ifr.contentWindow.location = "http://a.com/c.html";
}
};
document.body.appendChild(ifr);
</script>
<!--页面B--> <script> window.name = ‘b‘; </script>
标签:log eve 页面 body attach htm 动态创建 content ati
原文地址:http://www.cnblogs.com/dylanthomas/p/6044898.html