每当 iframe 重新加载时都需要回调
这类似于IFRAME完成加载时的Javascript回调?,但是不同之处在于,我采用了我们正在使用的基于 Web 的应用程序,为了对其进行一些监视,我将其放入 iframe 中,以便我的 javascript 可以查看网页内部。
因此,当用户使用该应用程序时,它将加载新页面,但它们都在 iframe 内,不会重新加载。
每当在此 iframe 中加载页面时,是否有某种方式的事件会通知我,或者至少在页面被替换时通知我?
This is similar to Javascript callback when IFRAME is finished loading?, but the difference is that I took a web-based application that we are using, and to do some monitoring of it I have put it inside of an iframe so my javascript can look inside the webpage.
So, as the user uses the application it will load new pages, but they are all within the iframe, which doesn't get reloaded.
Is there some way event that will inform me whenever a page gets loaded within this iframe, or, to at least be informed when the page is being replaced?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有这样的事件。我唯一的想法是运行计时器 (
window.SetInterval()
),定期检查 iframe 的位置 (URL) 与之前的检查相比是否发生了变化。还有一个想法:iframe 的内容是在您的控制之下还是您正在查看来自其他域的内容?如果是您的,您当然可以在该代码中放置事件,例如“
onbeforeunload
”,它会通知您 iframe 是否被重定向。There is no such event. The only idea I have in mind is to have timer running (
window.SetInterval()
) that regularly checks if the location (URL) of the iframe has changed compared to the previous check.Just another thought: is the content of the iframe under you control or are you viewing content from some other domain? If it's your's, you can of course place events inside that code like "
onbeforeunload
" which would inform you if the iframe is redirected.您可以控制那些在 iframe 中加载的页面吗?如果是这样,那么您可以向所有这些文件添加一个仅执行以下操作的 js 文件:
然后在主页面中,您拥有 iframe 的地方有这样的内容:
要使此功能正常工作,您还需要从以下位置提供 iframe 中的所有页面:与主页相同的域。
编辑
由于您从与主页面相同的域提供 iframe 页面,因此您需要控制为页面提供服务的服务器,因此即使您无法修改代码,也可以在“运行时”修改它" 当服务器返回内容时。例如,您可以返回 js 文件的修改版本,该文件始终加载诸如 parent.loaded(document.location.href); 之类的内容。
这可能看起来比使用 setTimeout 更复杂,但它仍然是另一个解决方案。
由于您有权访问 dom,您可以做的另一件事是注册 onunload 事件< /a> 这样,当您确定页面即将重新加载时,您可以使用 setTimeout,这将防止计时器持续不断。
话虽如此,也许这不是实施这种监控的最佳方式?也许你可以用浏览器扩展来实现这个?那些有幸了解所有文档位置变化的人。
另一种编辑
您可能想尝试对 iframe 使用此 onload 事件
Do you have control over those pages that get loaded inside the iframe? If so then you can just add to all of them a js file that only does this:
Then in the main page, where you have the iframe have this:
For this to work you also need to serve all the pages in the iframe from the same domain as the main page.
EDIT
Since you serve the iframe pages from the same domain as the main one, you need to have control of the server that is serving the pages, so even if you can't modify the code, you can modify it at "run-time" when the server return the content. You can for example return a modified version of a js file that is always loaded with something like parent.loaded(document.location.href);.
This might seem more complicated than using setTimeout, but it's another solution none the less.
Another thing that you can do, since you have access to the dom, is to register to the onunload event so that you can use setTimeout when you know for sure that the page is about to be reloaded, that will prevent a continues none stopping timer.
With all of that said, maybe this is not the best way to implement this monitoring? Maybe you can implement this with a browser extension? Those privileged to be aware of changes in locations of all the documents.
Another Edit
You might want to try to use this onload event for iframes