检测使用 window.open 打开的窗口的 onload 事件 [英] Detecting the onload event of a window opened with window.open

查看:83
本文介绍了检测使用 window.open 打开的窗口的 onload 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 window.popup = window.open($(this).attr('href'), 'Ad', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
 $(window.popup).onload = function()
        {
                alert("Popup has loaded a page");
        };

这在我尝试过的任何浏览器(IE、Firefox、Chrome)中都不起作用.如何检测页面何时加载到窗口中(如 iframe onload)?

This doesn't work in any browser I've tried it with (IE, Firefox, Chrome). How can I detect when a page is loaded in the window (like an iframe onload)?

推荐答案

如果弹出窗口的文档来自不同的域,这是不可能的.

If the pop-up's document is from a different domain, this is simply not possible.

2015 年 4 月更新:我错了:如果您拥有两个域,您可以使用 window.postMessage几乎所有与当今相关的浏览器.

Update April 2015: I was wrong about this: if you own both domains, you can use window.postMessage and the message event in pretty much all browsers that are relevant today.

如果没有,如果没有将文档加载到弹出窗口中的帮助,您仍然无法使这项工作跨浏览器工作.您需要能够检测弹出窗口加载后发生的更改,这可能是弹出页面中的 JavaScript 在处理自己的 load 事件时设置的变量,或者,如果您有一些控制权,您可以在 opener 中添加对函数的调用.

If not, there's still no way you'll be able to make this work cross-browser without some help from the document being loaded into the pop-up. You need to be able to detect a change in the pop-up that occurs once it has loaded, which could be a variable that JavaScript in the pop-up page sets when it handles its own load event, or if you have some control of it you could add a call to a function in the opener.

这篇关于检测使用 window.open 打开的窗口的 onload 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆