子页面如何判断父页面方法加载完毕

用了 x-admin 的框架, iframe 布局。其中有个 x-admin.js 文件默认引入了 element,layer,jquery 三个 js ,所以我想在子页面调用父页面的方法,这样就不需要重复引入相同的 js 文件了,但是子页面加载时父页面的 jquery 可能还没加载完毕,如何去处理这个问题呢?我目前想到的是用定时器每隔一段时间去检查,有什么更好的方法

阅读 2.6k
2 个回答

可以用父页面的最后一个方法执行完成后回调子页面上的方法,告诉子页面父页面处理好

你可以尝试使用window.postMessage方法,在父子页面完成特定任务后发送消息回去

Thewindow.postMessage()method safely enables cross-origin communication betweenWindowobjects;_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.,_viatargetWindow = window.opener), and then dispatch aMessageEventon 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.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题