iframe加载后调用指定方法onload=““

				<%
					String strSRC = SystemPath.SYSTEMPATH+"/scripts/common/view/File_View_Template.jsp?viewid=8a487e707a31bc51017a31cbf5ca0002&condition=t.invoice_id='"+oFile.getFILEID()+"'";
				%>
				<iframe id="inv_desc" width="100%" height="300" frameborder="0" scrolling="no" src="<%=strSRC%>" title="<%=strSRC%>" onload="setButtonDisplay()"  style="margin:5px">
				</iframe>

### 如何在 iframe 子页面 onload 完成调用父页面方法 为了实现这一功能,可以利用 JavaScript 的 `window.onload` 和跨文档通信机制来完成。以下是具体的做法: #### 方法描述 当子页面(即嵌套在 `<iframe>` 中的内容)加载完成后,可以通过设置子页面中的 `window.onload` 事件监听器,在该事件触发时通过 `parent` 或 `window.parent` 对象访问并调用父页面的方法[^1]。 #### 实现方式 以下是一个具体的例子展示如何实现此需求: ##### 父页面 (main.html) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Parent Page</title> <script type="text/javascript"> function parentMethod() { console.log('The child page has finished loading and called the method on the parent.'); } </script> </head> <body> <h1>This is the Parent Page</h1> <iframe id="myIframe" src="childPage.html"></iframe> </body> </html> ``` ##### 子页面 (childPage.html) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Child Page</title> <script type="text/javascript"> window.onload = function () { // 当子页面完全加载完毕后,调用父页面上的方法 if (window.parent && typeof window.parent.parentMethod === 'function') { window.parent.parentMethod(); } else { console.error('Parent method not found or inaccessible'); } }; </script> </head> <body> <h1>This is the Child Page</h1> </body> </html> ``` 在这个示例中,一旦子页面成功加载,它会自动检测是否存在名为 `parentMethod` 的函数,并尝试在其父窗口上调用这个函数[^4]。 #### 注意事项 - **安全性考虑**: 如果父子页面位于不同的域上,则可能受到同源策略的影响而无法互相访问对方的对象或属性。 - **错误处理**: 应始终检查目标对象及其成员的存在性和可访问性,以防止运行期异常发生[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值