关闭弹出窗口

发布于 2024-12-11 02:59:44 字数 490 浏览 0 评论 0原文

我找到了一个负责打开我需要的弹出窗口的代码:

 function winOpen(theURL, Name, popW, popH, scroll) { // V 1.0
 var winleft = (screen.width - popW) / 2;
 var winUp = (screen.height - popH) / 2;
 winProp =
'width='+popW+',height='+popH+',left='+winleft+',t op='+winUp+',scrollbars='+scroll+','
  Win = window.open(theURL, Name, winProp)
    if (parseInt(navigator.appVersion) >= 4){
      Win.window.focus();
   }

是否可以在此处包含在关闭窗口时提交的函数,或者是否需要在某处进行单独的函数调用?我在 html 链接上调用打开窗口。

I found a code that takes care of opening popup window I need:

 function winOpen(theURL, Name, popW, popH, scroll) { // V 1.0
 var winleft = (screen.width - popW) / 2;
 var winUp = (screen.height - popH) / 2;
 winProp =
'width='+popW+',height='+popH+',left='+winleft+',t op='+winUp+',scrollbars='+scroll+','
  Win = window.open(theURL, Name, winProp)
    if (parseInt(navigator.appVersion) >= 4){
      Win.window.focus();
   }

Is it possible to include here functions, that are commited when the window is closed, or does it require a separate function call somewhere? I'm calling window open on a html link.

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

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

发布评论

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

评论(2

却一份温柔 2024-12-18 02:59:44

您在弹出窗口中显示的页面可能有一个附加到 bodyonunloadonbeforeunload 的事件处理程序(我认为这仅适用于 IE) )。如果文档卸载(谁会猜到这一点?),它将触发。如果文档内没有用于卸载该文档的链接,则可以将此事件视为窗口已关闭的指示符。

第二种可能性:添加看门狗。

var watcherID = setInterval(function()
{
  if (Win.closed)
  {
    clearTimeout(watcherID);
    // Do what you have to do.
  }
}, 400);

The page you are showing inside the popup could have an event handler attached to the body's onunload or onbeforeunload (I think that is IE only). That will fire if the document unloads (who would have guessed that?). If you don't ave links inside the document that will unload it, you can take this event as an indicator that your window has been closed.

Second possibility: add a watchdog.

var watcherID = setInterval(function()
{
  if (Win.closed)
  {
    clearTimeout(watcherID);
    // Do what you have to do.
  }
}, 400);
却一份温柔 2024-12-18 02:59:44

您应该稍微缩进代码以使其更具可读性。另外,您可以使用 win.close 查看它是否已关闭 onunload,但请确保在检查之前稍等一下:http://jsfiddle.net/9PYWj/

function winOpen(theURL, Name, popW, popH, scroll) { // V 1.0
    var winleft = (screen.width - popW) / 2,
        winUp   = (screen.height - popH) / 2,

        winProp = 'width=' + popW 
                   + ',height=' + popH 
                   + ',left=' + winleft 
                   + ',top=' + winUp 
                   + ',scrollbars=' + scroll + ',',

        win     = window.open(theURL, Name, winProp);

    win.onunload = function() {
        setTimeout(function() {
            if(win.closed) {
                alert(123);
            }
        }, 100);
    };

    win.focus();
}

You should indent your code a little to make it more readable. Also, you can use win.closed to see it's closed onunload, but make sure to wait a little before checking: http://jsfiddle.net/9PYWj/.

function winOpen(theURL, Name, popW, popH, scroll) { // V 1.0
    var winleft = (screen.width - popW) / 2,
        winUp   = (screen.height - popH) / 2,

        winProp = 'width=' + popW 
                   + ',height=' + popH 
                   + ',left=' + winleft 
                   + ',top=' + winUp 
                   + ',scrollbars=' + scroll + ',',

        win     = window.open(theURL, Name, winProp);

    win.onunload = function() {
        setTimeout(function() {
            if(win.closed) {
                alert(123);
            }
        }, 100);
    };

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