iframe框架之间js方法相互调用及数据传递

本文通过一个具体的HTML页面示例,展示了如何在一个包含两个iframe的顶级页面top.html中实现跨iframe的JavaScript函数调用。主要介绍了如何从顶级页面调用子iframe页面中的函数,以及从一个iframe页面调用另一个iframe页面中的函数。

在一个叫top.html的页面定义两个iframe和两个方法

复制代码
<iframe id="gg" name="gg" src="gg.html" width="500" height="200"></iframe>

<iframe id="dd" name="dd" src="dd.html" width="500" height="200"></iframe>

<script type="text/javascript">
   function top()
   {
    alert("top");
    }   
//调用子框架gg.html页面中的gg函数,并把“我在top里呼唤gg”传给gg() function top_gg(){ window.frames["gg"].gg("我在top里呼唤gg"); } </script>
复制代码

在gg.html写两个方法调用

复制代码
<script type="text/javascript">
//调用顶层框架中的top函数
function() gg_top{
     top.top();
     })
function gg(t){
    alert(t);
    }
</script>
复制代码

在dd.html写一个方法调用

<script type="text/javascript">
     //在dd.html中调用gg.html中dd_gg()function() dd_gg{
     parent.window.frames["gg"].gg("在dd中呼唤gg");
     })
</script>

 

主子应用之间通过 JavaScript 方法相互调用的实现方法,会因同域和跨域场景的不同而有所差异: ### 同域通信 同域下,主应用和子应用可以通过直接访问彼此的 `window` 对象来进行通信。主应用可以通过 `iframe.contentWindow` 访问子应用的 `window` 对象,而子应用可以通过 `window.parent` 访问主应用的 `window` 对象。以下是一个示例代码: #### 主应用 HTML 文件(`main.html`) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Main App</title> </head> <body> <iframe id="myIframe" src="sub.html" frameborder="0"></iframe> <button onclick="callSubAppMethod()">Call Sub App Method</button> <script> function mainAppMethod() { alert('This is a method in the main app.'); } function callSubAppMethod() { const iframe = document.getElementById('myIframe'); iframe.contentWindow.subAppMethod(); } </script> </body> </html> ``` #### 子应用 HTML 文件(`sub.html`) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sub App</title> </head> <body> <button onclick="callMainAppMethod()">Call Main App Method</button> <script> function subAppMethod() { alert('This is a method in the sub app.'); } function callMainAppMethod() { window.parent.mainAppMethod(); } </script> </body> </html> ``` ### 跨域通信 跨域时,由于浏览器的同源策略,不能直接访问彼此的 `window` 对象,可使用 `postMessage` 方法,它是 HTML5 中引入的一种跨文档消息传递技术,允许在不同源之间传递数据。以下是一个示例代码: #### 主应用 HTML 文件(`main.html`) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Main App</title> </head> <body> <iframe id="myIframe" src="http://subapp.com/sub.html" frameborder="0"></iframe> <button onclick="sendMessageToSubApp()">Send Message to Sub App</button> <script> function mainAppMethod() { alert('This is a method in the main app.'); } function sendMessageToSubApp() { const iframe = document.getElementById('myIframe'); iframe.contentWindow.postMessage('callSubAppMethod', 'http://subapp.com'); } window.addEventListener('message', (event) => { if (event.origin === 'http://subapp.com') { if (event.data === 'callMainAppMethod') { mainAppMethod(); } } }); </script> </body> </html> ``` #### 子应用 HTML 文件(`sub.html`) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sub App</title> </head> <body> <button onclick="sendMessageToMainApp()">Send Message to Main App</button> <script> function subAppMethod() { alert('This is a method in the sub app.'); } function sendMessageToMainApp() { window.parent.postMessage('callMainAppMethod', 'http://mainapp.com'); } window.addEventListener('message', (event) => { if (event.origin === 'http://mainapp.com') { if (event.data === 'callSubAppMethod') { subAppMethod(); } } }); </script> </body> </html> ``` ### 使用微前端框架实现通信 在微前端架构中,如 qiankun 框架,也提供了主子应用通信的方式。qiankun 可以通过 `initGlobalState`、`setGlobalState`、`onGlobalStateChange` 等方法来实现主子应用之间的状态管理和通信。注意,`initGlobalState`、`setGlobalState`、`onGlobalStateChange` 只能在主应用中注册,`setGlobalState`、`onGlobalStateChange` 在子应用中通过 `props` 传值在子应用入口 `js` 文件导出的 `mount` 生命周期中接收调用 [^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值