跨窗口 JavaScript 事件
我正在使用 window.open(...) 启动一个弹出窗口,并将一个 elementId 传递给新的弹出窗口。
然后在弹出窗口启动期间,我在开启器窗口中找到与传递给弹出窗口的 elementId 匹配的元素。然后,弹出窗口使用 jQuery.bind(...) 订阅该元素上的事件。然后从打开器窗口内部,我使用 jQuery.trigger(...) 触发这些事件,我还尝试了triggerHandlers。
问题是我的弹出窗口的事件处理程序永远不会被调用。我可以从开启器窗口内订阅事件,没有问题。但是,当我从弹出窗口尝试时,它不起作用。
有人对如何解决这个问题有任何想法吗?这是某种安全描述吗?
非常感谢您的阅读!
I am launching a popup window with window.open(...) and I pass an elementId to the new popup window.
Then during startup of the popup window I find the element in the opener window that matches the elementId passed to the popup. Then the popup subscribes to events on that element using jQuery.bind(...). Then from inside the opener window I fire these events using jQuery.trigger(...), I also tried triggerHandlers.
The problem is that my popup's eventHandlers never get called. I can subscribe to the events from within inside the opener window no problem. However, when I try from the popup, it doesn't work.
Does anyone have any ideas on how to fix this? Is this some kind of security description?
Thanks a lot for reading!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,当我找到“opener”页面元素并以这种方式分配处理程序时:
然后,令我惊讶的是,本机“真实”事件工作得很好。 但是,从打开页面触发的自定义事件不会被弹出页面获取。这是有道理的,因为每个页面都有自己的小 jQuery 世界。然而,关于浏览器不传播本机事件,我显然是错误的,所以感谢今天的学习经验!
更多信息 - 从弹出窗口(以及类似地从主文档的任何子
)中,您还可以
在打开窗口中查找内容。然而,仅仅使用弹出页面中的 jQuery 对象来查找该元素是行不通的,因为 jQuery 不会遍历“window.opener”链接并在那里寻找该元素。当您在弹出页面上调用
$('#thingId')
时,jQuery 将使用document< 调用
document.getElementById('thingId')
弹出页面的 /code> 对象。如果该页面上没有名为“thingId”的元素,则不会找到它。原始答案:
我认为您尝试做的事情不会起作用。浏览器不会在与包含目标元素的窗口不同的窗口中触发任何事件处理程序。
但是,您可以在一个窗口中捕获事件,然后在另一个窗口中触发自定义事件。当您这样做时,您可能希望通过该页面上的 jQuery 对象触发事件。换句话说,你会这样做:
OK, when I find the "opener" page element and assign handlers this way:
Then, to my surprise, native "real" events work just fine. However, custom events fired from the opener page do not get picked up by the popup page. That sort of makes sense, as each page has its own little jQuery universe. I was apparently wrong however about the browser not propagating native events, so thanks for today's learning experience!!
more info — From the popup window (and similarly from any child
<iframe>
of the main document), you can also useto find stuff in the opener window. However, simply using the jQuery object in the popup page to find that element cannot work, because jQuery will not traverse the "window.opener" link and go hunting for the element there. When you call
$('#thingId')
on the popup page, jQuery is just going to calleddocument.getElementById('thingId')
using thedocument
object for the popup page. If there's no element called "thingId" on that page, it won't be found.original answer:
I don't think that what you're trying to do will work. The browser is not going to trigger any event handlers in a window different from the one containing the target element.
You can, however, catch the event in one window and then trigger a custom event in the other window. When you do that, you're probably going to want to trigger the event through the jQuery object on that page. In other words, you'd do this:
感谢 Pointy 的回答。我对此进行了实验并想分享我的发现。实际上,您可以在弹出窗口中定义事件侦听器,而无需引用开启器:
在作为弹出窗口打开的页面中:
在打开弹出窗口的页面中:
秘密是:甚至如果您从弹出窗口对象调用 jQuery,则不能直接使用选择器,因为 jQuery 将尝试在父(打开器)窗口中查找元素。
Thanks to Pointy for the answer. I experimented with this and want share my findings. You can actually define your event listeners in the popup window without referencing the opener:
in the page opened as popup:
in the page which opens the popup:
The secret is: even if you invoke jQuery from popup window object, you can't use selectors directly, because jQuery will try to find elements in the parent (opener) window.