iframe 跨域访问以及操作

来源:互联网 发布:学理科的软件 编辑:程序博客网 时间:2024/06/05 17:18

按情境分
1、不跨域时
2、主域相同、子域不同时
3、主域不同



访问iframe: contentWindow
访问父级:parent

跨域时

1. 是同主域下面,不同子域之间的跨域

如:xx.tecent.com与yy.tecent.com之间有同主域tecent.com

同主域,不同子域跨域,设置相同的document.domian就可以解决;

例如:

http://a.tecent.com/main.html

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title></head><body><iframe src="http://b.tecent.com/iframe.html"  id=“iframe”></iframe><script>  document.domain="tecent.com";
   var name="main.html";
//访问iframe里的元素
<span style="color: rgb(237, 28, 36); font-family: simsun; font-size: 14px; line-height: 21px;">document.getElementById("iframe").contentWindow.document.getElementById("</span><span style="color: rgb(237, 28, 36); font-family: simsun;">childNode</span><span style="color: rgb(237, 28, 36); font-family: simsun; line-height: 21px;">").value</span><span style="color: rgb(237, 28, 36); font-family: simsun; line-height: 21px;">;</span><span style="color: rgb(237, 28, 36); font-family: simsun; font-size: 14px; line-height: 21px;"></span></script></body></html>

http://b.tecent.com/iframe.html

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title></head><body><span style="white-space:pre"></span><input  id="childNode" value="这是iframe页面内容"/><script>  document.domain="tecent.com";//访问iframe外页面的元素
alert(parent.name);
</script></body></html>

2.不同主域跨域

暂时没遇到,等以后再补上




0 0
原创粉丝点击