iframe与主框架跨域相互访问方法
iframe 与主框架相互访问方法
http://blog.csdn.net/fdipzone/article/details/17619673/
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
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title> main window </title>
- <script type="text/javascript">
- // main js function
- function fMain(){
- alert('main function execute success');
- }
- // exec iframe function
- function exec_iframe(){
- window.myframe.fIframe();
- }
- </script>
- </head>
- <body>
- <p>A.html main</p>
- <p><input type="button" value="exec iframe function" onclick="exec_iframe()"></p>
- <iframe src="B.html" name="myframe" width="500" height="100"></iframe>
- </body>
- </html>
B.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title> iframe window </title>
- <script type="text/javascript">
- // iframe js function
- function fIframe(){
- alert('iframe function execute success');
- }
- // exec main function
- function exec_main(){
- parent.fMain();
- }
- </script>
- </head>
- <body>
- <p>B.html iframe</p>
- <p><input type="button" value="exec main function" onclick="exec_main()"></p>
- </body>
- </html>
点击A.html 的 exec iframe function button,执行成功,弹出iframe function execute success。如下图
点击B.html 的 exec main function button,执行成功,弹出 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已经不同一个域,因此执行效果是一样的。
实际使用时换成 www.domaina.com 与 www.domainb.com 即可。
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 与 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 如何调用B.html的 fIframe方法
1.在A.html 创建一个 iframe
2.iframe的页面放在 B.html 同域下,命名为execB.html
3.execB.html 里有调用B.html fIframe方法的js调用
- <script type="text/javascript">
- parent.window.myframe.fIframe(); // execute parent myframe fIframe function
- </script>
这样A.html 就能通过 execB.html 调用 B.html 的 fIframe 方法了。
同理,B.html 需要调用A.html fMain方法,需要在B.html 嵌入与A.html 同域的 execA.html
execA.html 里有调用 A.html fMain 方法的js 调用
- <script type="text/javascript">
- parent.parent.fMain(); // execute main function
- </script>
这样就能实现 A.html 与 B.html 跨域相互调用。
A.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title> main window </title>
- <script type="text/javascript">
- // main js function
- function fMain(){
- alert('main function execute success');
- }
- // exec iframe function
- function exec_iframe(){
- if(typeof(exec_obj)=='undefined'){
- exec_obj = document.createElement('iframe');
- exec_obj.name = 'tmp_frame';
- exec_obj.src = 'http://127.0.0.1/execB.html';
- exec_obj.style.display = 'none';
- document.body.appendChild(exec_obj);
- }else{
- exec_obj.src = 'http://127.0.0.1/execB.html?' + Math.random();
- }
- }
- </script>
- </head>
- <body>
- <p>A.html main</p>
- <p><input type="button" value="exec iframe function" onclick="exec_iframe()"></p>
- <iframe src="http://127.0.0.1/B.html" name="myframe" width="500" height="100"></iframe>
- </body>
- </html>
B.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title> iframe window </title>
- <script type="text/javascript">
- // iframe js function
- function fIframe(){
- alert('iframe function execute success');
- }
- // exec main function
- function exec_main(){
- if(typeof(exec_obj)=='undefined'){
- exec_obj = document.createElement('iframe');
- exec_obj.name = 'tmp_frame';
- exec_obj.src = 'http://localhost/execA.html';
- exec_obj.style.display = 'none';
- document.body.appendChild(exec_obj);
- }else{
- exec_obj.src = 'http://localhost/execA.html?' + Math.random();
- }
- }
- </script>
- </head>
- <body>
- <p>B.html iframe</p>
- <p><input type="button" value="exec main function" onclick="exec_main()"></p>
- </body>
- </html>
execA.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title> exec main function </title>
- </head>
- <body>
- <script type="text/javascript">
- parent.parent.fMain(); // execute main function
- </script>
- </body>
- </html>
execB.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title> exec iframe function </title>
- </head>
- <body>
- <script type="text/javascript">
- parent.window.myframe.fIframe(); // execute parent myframe fIframe function
- </script>
- </body>
- </html>
执行如下图:
源码下载地址:点击查看
iframe与主框架跨域相互访问方法的更多相关文章
- iframe与主框架跨域相互访问方法【转】
转自:http://blog.csdn.net/fdipzone/article/details/17619673 1.同域相互访问 假设A.html 与 b.html domain都是localho ...
- 计算机网络之iframe内联框架跨域
iframe框架同源下的数据调用 iframe框架非同源下的数据传输 一.iframe框架同源下的数据调用 1.父窗口向子窗口获取数据 //html1父级窗口 <iframe src=" ...
- js 利用iframe和location.hash跨域解决的方法,java图片上传回调JS函数跨域
奶奶的:折腾了我二天,最终攻克了!网上有非常多样例. 但跟我的都不太一样,费话不多说了,上图 上代码: IE ,firefix,chrome 測试通过 js :这个主页面,部分代码, functi ...
- CORSFilter 跨域资源访问
CORS 定义 Cross-Origin Resource Sharing(CORS)跨来源资源共享是一份浏览器技术的规范,提供了 Web 服务从不同域传来沙盒脚本的方法,以避开浏览器的同源策略,是 ...
- JAVA跨域资源访问CORSFilter
当一个资源从与该资源本身所在的服务器不同的域或端口不同的域或不同的端口请求一个资源时,资源会发起一个跨域 HTTP 请求. 出于安全考虑,浏览器会限制从脚本内发起的跨域HTTP请求.跨域资源共享机制允 ...
- IIS Manager 配置文件修该,允许跨域CORS访问
IIS Manager 配置文件修该,允许跨域CORS访问 IIS Manager 的api访问会出现跨域问题,需要 IIS Manager的配置文件中修改. 配置文件的路径:C:\Program F ...
- Tornado—添加请求头允许跨域请求访问
跨域请求访问 如果是前后端分离,那就肯定会遇到cros跨域请求难题,可以设置一个BaseHandler,然后继承即可. class BaseHandler(tornado.web.RequestHan ...
- nginx 跨域请求访问
1.nginx跨域请求访问 location ~ .*\.(htm|html)$ { add_header Access-Control-Allow-Origin(请求域名) *(所有域名) http ...
- 解决 Golnag Gin框架跨域
package main import ( "github.com/gin-gonic/gin" "awesomeProject/app/app_routers" ...
随机推荐
- es6写法
我们在日常开发中,如果我们使用es5则可以直接在浏览器里面写JavaScript脚本.一点问题也没有. 但是在写es6语法的JavaScript代码的时候,我们就需要引入babel翻译器了. 例如: ...
- struct详解
正常定义一个数据结构都是这样用 typedef struct{ int a; int b; }M; 在使用时 M a; 其实 struct是这样的 struct M{ int a; int b; }; ...
- Coursera, Deep Learning 4, Convolutional Neural Networks - week1
CNN 主要解决 computer vision 问题,同时解决input X 维度太大的问题. Edge detection 下面演示了convolution 的概念 下图的 vertical ed ...
- ng directive compile pre-link post-link
原文链接: http://www.jb51.net/article/58229.htm 1.ng在link之前编译所有的指令,然后link又分为 pre-link 与 post-link阶段compi ...
- JSR
JSR是Java Specification Requests的缩写,意思是Java 规范提案.是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求.任何人 ...
- Codeforces Round #545 (Div. 2)(D. Camp Schedule)
题目链接:http://codeforces.com/contest/1138/problem/D 题目大意:给你两个字符串s1和s2(只包含0和1),对于s1中,你可以调换任意两个字符的位置.问你最 ...
- 简单日历dom
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- 51nod 1232 完美数
题目思路:数位dp,若这个数能被每位的非0数整除,那么这个数一定可以被每一位数的lcm整除,lcm(1,2,3,4,5,6,7,8,9) = 2520,所以可以通过将这个数对2520取模来压缩空间,取 ...
- Centos7配置静态IP后无法ping通外部网络的问题(无法上网)
打开ifcfg-ens33配置文件,注意下面标记部分 建议: (1)ifcfg-ens33 DNS 配置 DNS1=8.8.8.8 DNS2=8.8.4.4 (2)配置DNS解析才能够识别外部的IP域 ...
- 20165234 《Java程序设计》第五周学习总结
第五周学习总结 教材学习内容总结 第七章 内部类与异常类 内部类 内部类:在一个类中定义另一个类. 外嵌类:包含内部类的类,称为内部类的外嵌类. 内部类的类体中不能声明类变量和类方法.外嵌类的类体中可 ...