iframe父子页面相互传值.docx
《iframe父子页面相互传值.docx》由会员分享,可在线阅读,更多相关《iframe父子页面相互传值.docx(8页珍藏版)》请在三一办公上搜索。
1、iframe父子页面相互传值iframe与主框架相互访问方法 1.同域相互访问 假设A.html 与 b.html domain都是localhost A.html中iframe嵌入 B.html,name=myframe A.html有js function fMain B.html有js function fIframe 需要实现 A.html 调用 B.html 的fIframe,B.html 调用 A.html 的fMain A.html html view plaincopy1. 2. 3. 4. 5. main window 6. 7. 8. / main js function
2、9. function fMain 10. alert(main function execute success); 11. 12. 13. / exec iframe function 14. function exec_iframe 15. window.myframe.fIframe; 16. 17. 18. 19. 20. 21. 22. A.html main 23. 24. 25. 26. B.html html view plaincopy1. 2. 3. 4. 5. iframe window 6. 7. 8. / iframe js function 9. function
3、 fIframe 10. alert(iframe function execute success); 11. 12. 13. / exec main function 14. function exec_main 15. parent.fMain; 16. 17. 18. 19. 20. 21. 22. B.html iframe 23. 24. 25. 点击A.html 的 exec iframe function button,执行成功,弹出iframe function execute success。如下图 点击B.html 的 exec main function button,
4、执行成功,弹出 main function execute success。如下图 2.跨域互相访问 假设 A.html domain是 localhost, B.html domain 是 127.0.0.1 这里使用localhost与 127.0.0.1 只是方便测试,localhost与 127.0.0.1已经不同一个域,因此执行效果是一样的。 实际使用时换成 与 即可。 A.html中iframe嵌入 B.html,name=myframe A.html有js function fMain B.html有js function fIframe 需要实现 A.html 调用 B.htm
5、l 的fIframe,B.html 调用 A.html 的fMain 如果使用上面同域的方法,浏览器判断A.html 与 B.html 不同域,会有错误提示。 Uncaught SecurityError: Blocked a frame with origin http:/localhost from accessing a frame with origin http:/127.0.0.1. Protocols, domains, and ports must match. 实现原理: 因为浏览器为了安全,禁止了不同域访问。因此只要调用与执行的双方是同域则可以相互访问。 首先,A.html
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- iframe 父子 页面 相互

链接地址:https://www.31ppt.com/p-3158693.html