Javascript与iframe的那些事儿
2012-02-01 09:28
288 查看
iframe 很多网站都在用,虽然方便开发与维护(可能同时有几个页面调用同一个
同域或跨子域读写操作 iframe 里的内容
父页面读写操作子页面:
注:在请务必通过
子页面读写操作父页面:
小结:
1 测试通过 IE6, IE7, IE8, Firefox2.0, Firefox3.0, Firefox3.6, Chrome8, Opera11, Safari5.
2 查阅资料得出 document.getElementById(‘id name’).contentDocument 全等于 window.frames["iframe Name"].document.
3 跨子域时,需要在父页面和子页面 JS 中分别加上
跨域操作 iframe 里内容
当两个网页所在域不同时,要实现数据的互相调用,只能通过 JS 改变 location 对象的 hash 属性的值来做到互相通信。
父页面:
子页面:
小结:
1 经测试 IE6, IE7, IE8, Firefox2.0, Firefox3.0, Firefox3.6, Chrome8, Opera11, Safari5, 除 IE6, IE7 外只要改变
2 我有试图在子页面用
2 子页面无权读取父页面的 url, 但可以对父页面的 url 进行写操作,所以跨域操作时,要提前得知父页面的url
由于前端解决跨域问题的局限性比较大,所以最好用
服务器端解决方案。
iframe),不过却存在安全问题。嵌入
iframe的页面,父页面与子页面均可以很轻松的在同域或跨子域的情况下进行读写操作;在完全不同域的情况下,也可以通过更改
hash的方式进行通信。下面我在九个不同(版本的)浏览器中对此进行数据传输与更改的兼容性测试。
同域或跨子域读写操作 iframe 里的内容
父页面读写操作子页面:
<iframe id="test-iframe" name="test-iframe" src="child.html" scrolling="no" frameborder="0"></iframe> <script> window.onload = function () { /* * 下面两种获取节点内容的方式都可以。 * 由于 IE6, IE7 不支持 contentDocument 属性,所以此处用了通用的 * window.frames["iframe Name"] or window.frames[index] */ var d = window.frames["test-iframe"].document; d.getElementsByTagName('h1')[0].innerHTML = 'pp'; alert(d.getElementsByTagName('h1')[0].firstChild.data); } </script>
注:在请务必通过
window.onload方法访问
iframe中的节点,否则浏览器会提示错误-拒绝访问。在 IE8, Firefox3.6, Opera11 下在
DOMReady时也可以访问
iframe中的节点。
子页面读写操作父页面:
<script> parent.document.getElementsByTagName('h1')[0].innerHTML = 'pp'; alert(parent.document.getElementsByTagName('h1')[0].firstChild.data); </script>
小结:
1 测试通过 IE6, IE7, IE8, Firefox2.0, Firefox3.0, Firefox3.6, Chrome8, Opera11, Safari5.
2 查阅资料得出 document.getElementById(‘id name’).contentDocument 全等于 window.frames["iframe Name"].document.
3 跨子域时,需要在父页面和子页面 JS 中分别加上
document.domain = 'xxx.com';
跨域操作 iframe 里内容
当两个网页所在域不同时,要实现数据的互相调用,只能通过 JS 改变 location 对象的 hash 属性的值来做到互相通信。
父页面:
<iframe id="test-iframe" src="http://www.yyy.com/child.html" scrolling="no" frameborder="0"></iframe> <input type="button" value="send" onclick="sendRequest()" /> <script> function sendRequest() { document.getElementById('test-iframe').src += '#a'; } var interval = window.setInterval(function(){ if(location.hash) { alert(location.hash); window.clearInterval(interval); } },1000); </script>
子页面:
<h1>RRRRRR</h1> <script> var url = 'http://www.xxx.com/father.html'; oldHash = self.location.hash, newHash, interval = window.setInterval(function(){ newHash = self.location.hash; if(oldHash != self.location.hash) { document.getElementsByTagName('h1')[0].innerHTML = 'pp'; //alert(parent.location.href); //去掉这个注释,浏览器会提示无权限 parent.location.href = url + '#b'; window.clearInterval(interval); } },500); </script>
小结:
1 经测试 IE6, IE7, IE8, Firefox2.0, Firefox3.0, Firefox3.6, Chrome8, Opera11, Safari5, 除 IE6, IE7 外只要改变
hash就会记录在浏览器
history中。
2 我有试图在子页面用
parent.location.replace的方法来避免父页面向服务器发送请求而进行跳转,这样理论上浏览器就不会记录历史,但未能奏效。
2 子页面无权读取父页面的 url, 但可以对父页面的 url 进行写操作,所以跨域操作时,要提前得知父页面的url
由于前端解决跨域问题的局限性比较大,所以最好用
服务器端解决方案。
相关文章推荐
- 关于Javascript与iframe的那些事儿
- Javascript与iframe的那些事儿
- 关于Javascript与iframe的那些事儿
- JavaScript打印iframe内容示例代码
- javascript+iframe 实现无刷新载入整页的代码
- 用JavaScript实现在iframe中操作父页image控件
- javascript iframe相关操作
- Javascript获取IFrame内容(兼容IE&FF)
- JavaScript对iframe的DOM操作
- JavaScript:全面解析Ajax跨站数据传输和iframe跨域名js调用
- javascript控制页面(含iframe进行页面跳转)跳转、刷新的方法汇总
- Arcgis Javascript那些事儿(七)--AMD详解
- 通过JavaScript获取iframe中元素的值
- [javascript] 判断 iframe 是否加载完成
- IE6通过点击 <a href="javascript:void(0);"></a>方式动态创建iframe无法显示
- iframe javascript:location.replace(url)路径问题
- javascript对iframe的互操作浅析
- Javascript之base.js原型那些事儿
- JavaScript应用:Iframe自适应其加载的内容高度
- javascript 父子iframe相互传参数