Iframe跨域访问
来源:互联网 发布:欧碧诗胶原蛋白知乎 编辑:程序博客网 时间:2024/05/23 13:36
原文地址:http://www.cnblogs.com/pigtail/archive/2013/01/24/2875310.html
js跨域是个讨论很多的话题。iframe跨域访问也被研究的很透了。
一般分两种情况:
一、 是同主域下面,不同子域之间的跨域;
同主域,不同子域跨域,设置相同的document.domian就可以解决;
父页访问子页,可以document.getElementById("myframe").contentWindow.document来访问iframe页面的内容;如果支持contentDocument也可以直接document.getElementById("myframe").contentDocument访问子页面内容;
子页访问父页,可以parent.js全局属性
二、 是不同主域跨域;
前提,www.a.com下a.html,a.html内iframe调用了www.b.com下的b.html,b.html下iframe调用了www.a.com下的c.html
b.html是无法直接访问a.html的对象,因为涉及到跨域,但可以访问parent,同样c.html的parent可以访问b.html。c.html和a.html同域,是可以访问a下的对象的。parent.parent.js对象!
看下面实例:
a.html
<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title></head><body><iframe src="http://www.b.com/b.html" ></iframe><ul id="getText"></ul><script> function dosome(text){ document.getElementById("getText").innerHTML= decodeURI(text); }</script></body></html>
b.html
<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title></head><body><iframe id="myfarme" src="###"></iframe><ul id="ct"><li>这里是内容1</li><li>这里是内容2</li><li>这里是内容3</li><li>这里是内容4</li><li>这里是内容5</li><li>这里是内容6</li></ul><script> window.onload = function(){ var text = document.getElementById('ct').innerHTML; document.getElementById('myfarme').src="http://www.a.com/c.html?content="+encodeURI(text); }</script></body></html>
c.html
<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title><script>window.onload = function(){ var text = window.location.href.split('=')[1] console.log(parent.parent) parent.parent.dosome(text);}</script></head><body>ddddddddddd</body></html>
阅读全文
0 0
- iframe跨域访问
- iframe跨域访问
- iframe跨域访问
- iframe跨域访问
- iframe跨域访问
- iframe跨域访问
- iframe跨域访问
- Iframe跨域访问
- iframe表单跨域访问
- iframe跨域访问问题
- iframe跨域互相访问
- iframe跨域访问cookie、Session
- iframe中页面跨域访问javascript
- JS跨域访问操作iframe
- JS跨域访问操作iframe
- iframe用法 及 跨域访问问题
- IE中iframe跨域访问
- iframe 跨域访问以及操作
- NOIP水题测试(2017082401)
- this指针的理解
- Universal 2nd Factor (U2F) 概述(2)-背景
- 初始化方法
- 【词汇详解】HTTP的几个重要概念
- Iframe跨域访问
- HDU6144 Arithmetic of Bomb
- 关于流通系统的流失特性的研究
- BZOJ 4800 [Ceoi2015]Ice Hockey World Championship ……
- Java-mysql-批量插入
- 【PAT】【Advanced Level】1072. Gas Station (30)
- js闭包(closure)
- Redis为什么是单线程
- Eclipse自动补全功能轻松设置