用了 x-admin
的框架, iframe
布局。其中有个 x-admin.js
文件默认引入了 element,layer,jquery
三个 js
,所以我想在子页面调用父页面的方法,这样就不需要重复引入相同的 js
文件了,但是子页面加载时父页面的 jquery
可能还没加载完毕,如何去处理这个问题呢?我目前想到的是用定时器每隔一段时间去检查,有什么更好的方法
用了 x-admin
的框架, iframe
布局。其中有个 x-admin.js
文件默认引入了 element,layer,jquery
三个 js
,所以我想在子页面调用父页面的方法,这样就不需要重复引入相同的 js
文件了,但是子页面加载时父页面的 jquery
可能还没加载完毕,如何去处理这个问题呢?我目前想到的是用定时器每隔一段时间去检查,有什么更好的方法
你可以尝试使用window.postMessage
方法,在父子页面完成特定任务后发送消息回去
Thewindow.postMessage()
method safely enables cross-origin communication betweenWindow
objects;_e.g.,_between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.Normally, scripts on different pages are allowed to access each other if and only if the pages they originate from share the same protocol, port number, and host (also known as the "same-origin policy").
window.postMessage()
provides a controlled mechanism to securely circumvent this restriction (if used properly).Broadly, one window may obtain a reference to another (_e.g.,_via
targetWindow = window.opener
), and then dispatch aMessageEvent
on it withtargetWindow.postMessage()
. The receiving window is then free tohandle this eventas needed. The arguments passed towindow.postMessage()
(_i.e.,_the “message”) areexposed to the receiving window through the event object.
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
可以用父页面的最后一个方法执行完成后回调子页面上的方法,告诉子页面父页面处理好