如何知道何时在JavaScript中关闭弹出窗口 [英] How to know when popup is closed in javascript

查看:96
本文介绍了如何知道何时在JavaScript中关闭弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码将在弹出窗口中打开go.com:

This code will open go.com in popup window:

<a href="javascript:void(0)" onClick="javascript:window.open('http://go.com','','status=yes,scrollbars=yes,toolbar=no,menubar=no,location=no ,width=300px,height=300px')">open</a>

,并且我想在主页中使用alert('closed!'),当弹出窗口关闭且我无法编辑使用onunload="alert('closed!')"的弹出页面时,因为弹出页面是外部页面,并且我无法更改代码,例如

and I want to use alert('closed!') in main page, when popup is closed and I can't edit popup page for use onunload="alert('closed!')", because the popup page is an external page and I can't change the code like

var mywindow = window.open('http://go.com') ...

谢谢.

推荐答案

如何打开包含包含外部页面内容的iframe的本地"窗口,而不是直接在窗口中加载该外部页面?这样,您仍然可以对实际窗口进行足够的控制,以知道何时关闭窗口.

How about instead of directly loading that external page in the window, you open up a "local" window with an iframe containing the external page content? That way you still have enough control over the actual window to be able to tell when it's closed.

某些缺点:地址栏不会显示他们正在浏览的URL,如果go.com具有某种框架破坏代码,它也可能会损坏.

Some drawbacks: the address bar will not show the URL they are browsing and it may also break if go.com has some sort of frame busting code.

一些示例代码:

function openIFrameInWindow(url) {
    var myWindow = window.open("");

    var iframe = document.createElement("iframe");
    iframe.src = url;

    myWindow.document.body.appendChild(iframe); 

    myWindow.document.body.onbeforeunload = function () { alert("WINDOW CLOSED") }; 
}

<a href="javascript:void(0)" onClick="javascript:openIFrameInWindow('http://go.com')">open</a>

这篇关于如何知道何时在JavaScript中关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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