为什么打开的窗口只触发一次“onload”事件?

发布于 2024-11-07 02:33:25 字数 666 浏览 4 评论 0原文

我一直在寻找问题的答案,但到目前为止还没有成功。

这是我的代码:

var oNewWindow = window.open(sLink,'_blank');
oNewWindow.addEventListener('load',function() { alert('Page loaded'); }, false);

当弹出窗口/选项卡打开时,处理程序被调用,但是如果我在弹出窗口内浏览,则永远不会调用处理程序。为什么?有没有办法让它发挥作用?

基本思想是用户打开一个弹出窗口,然后开始在弹出窗口中浏览,而父窗口中的代码继续工作并监视弹出窗口中发生的情况。弹出的页面是同域的页面。

第一次加载弹出窗口后,我向其附加了一个 onunload 处理程序,该处理程序将调用父窗口中的处理程序,然后计时器将启动。然后父窗口的脚本将处理弹出窗口的内容。然而,问题是滞后,如果计时器耗尽并且弹出窗口内的旧页面仍然存在,我会得到不正确的数据,并且整个脚本都会停止。此后我无法附加另一个 onunload 处理程序。

onload 处理程序是为整个窗口设置的,而不是为窗口内的文档设置的,那么为什么不调用该处理程序呢?

另外,我的脚本是在 GreaseMonkey 的帮助下运行的。我不需要跨浏览器,因为我只使用 Firefox。

I've been searching for an answer to my problem—with no luck so far.

Here's my code:

var oNewWindow = window.open(sLink,'_blank');
oNewWindow.addEventListener('load',function() { alert('Page loaded'); }, false);

When the popup window/tab opens the handler is called, but if I browse inside the popup window, the handler is never called. Why? And is there a way to make it work?

The base idea is that user opens a popup window and then starts browsing in the popup, while the code in parent window continues working and monitoring what is happening in the popup. The popup page is the page on the same domain.

After loading the popup for the first time, I attached an onunload handler to it, which would call the handler in the parent window and then the timer would start. Then the parent window's script would work with the contents of the popup. However, the problem comes with lag, if the timer runs out and the old page inside popup is still there, I get incorrect data and my whole script stops. I am unable to attach another onunload handler after that.

The onload handler is set for the window as the whole, not a document within the window, so why isn't the handler called?

Also, my script is run with the help of GreaseMonkey. I don't need this to be cross-browser since I only work with Firefox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪梦之恋 2024-11-14 02:33:25

一旦用户离开原始页面,打开的窗口就不再具有打开窗口应用的 load 事件,这就是该警报不会再次触发的原因。

您可以使用的一种方法是让页面将所需的 url 封装在 iframe (www.mypage.com/myframe?url=someurl.com) 中,以便子窗口内的页面不会改变,但iframe的内容会改变。从这里您可以告诉 window.openeriframe 中发生了什么。

Once the user navigates away from that original page, the opened window no longer has the load event applied by the opening window, which is why that alert does not fire again.

One approach you could use is to have a page that wraps the desired url within an iframe (www.mypage.com/myframe?url=someurl.com) so that the page within the child window does not change, but the contents of the iframe change. From here you can tell the window.opener what is going on from within the iframe.

旧梦荧光笔 2024-11-14 02:33:25

原因是当浏览器导航到新页面时,会为该页面初始化一个新的 window 对象。这可能会令人困惑,但最好将 window 对象视为与操作系统上运行的进程所拥有的窗口无关。

总而言之,父窗口和打开的窗口之间的任何关系在导航后都会丢失。

The reason is because when the browser navigates to a new page, a new window object is initialised for that page. This may be confusing, but it's best to think of the window object as being unrelated to a window owned by a process running on your operating system.

To summarize, any relationship between the parent window and the opened window is lost after navigation.

你的往事 2024-11-14 02:33:25

另一种选择是将脚本块插入到子窗口中,然后执行“加载”事件的附加。

这样,它就完全独立于父窗口。

Another alternative would be inserting a script block into the child window, which would then perform the attaching of the "load" event.

That way, it becomes completely independent of the parent window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文