当焦点离开时如何关闭弹出窗口

发布于 2024-12-28 15:36:29 字数 1307 浏览 2 评论 0 原文

要求:将从父窗口打开一个弹出窗口,并且当窗口失去焦点时应将其关闭。 (即使打开另一个应用程序窗口或成为焦点,也会发生这种情况)。

尝试过的代码:

问题: 在弹出窗口主体内单击时,弹出窗口将关闭。

兼容性: ie6及以上、firefox。

我从解决方法 rel="nofollow">http://pro-thoughts.blogspot.com/2006/10/in Correct-behavior-of-windowonblur.html

var active_element;  
var bIsMSIE;

function initiateSelfClosing() {  
  if (navigator.appName == "Microsoft Internet Explorer") {  
    active_element = document.activeElement;  
    document.onfocusout = closeWnd;  
    bIsMSIE = true;  
  }  
  else { window.onblur = closeWnd; }  
}

function closeWnd() {  
  if (window.opener != null) {  
    if (bIsMSIE && (active_element != document.activeElement)) {  
      active_element = document.activeElement;  
    }  
    else {  
      window.close();  
    }  
  }  
}  


<body onload="initiateSelfClosing()">  
</body>

但这里也存在一个问题,如果页面中有打印按钮,并且如果我单击“打印>”然后取消打印作业,弹出窗口将关闭。

有人可以帮我吗...

Requirement: A popup will be opened up from parent window, and it should get closed when the focus from the window is lost. (This should happen even when another application window is opened or came into focus).

Tried code:
<body onBlur='javascript:window.close();'>

Issue:
On click within the body of the popup makes the popup closed.

Compatibility: ie6 and above, firefox.

I got a workaround from http://pro-thoughts.blogspot.com/2006/10/incorrect-behavior-of-windowonblur.html

var active_element;  
var bIsMSIE;

function initiateSelfClosing() {  
  if (navigator.appName == "Microsoft Internet Explorer") {  
    active_element = document.activeElement;  
    document.onfocusout = closeWnd;  
    bIsMSIE = true;  
  }  
  else { window.onblur = closeWnd; }  
}

function closeWnd() {  
  if (window.opener != null) {  
    if (bIsMSIE && (active_element != document.activeElement)) {  
      active_element = document.activeElement;  
    }  
    else {  
      window.close();  
    }  
  }  
}  


<body onload="initiateSelfClosing()">  
</body>

But here also one problem is there, if there is a print button in the page, and if am clicking on print > then cancelling the print job, the popup is getting closed.

Can some one help me pls...

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

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

发布评论

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

评论(3

眼前雾蒙蒙 2025-01-04 15:36:29

sathishkumar 的答案中的一些修正

1.我们需要将 win 转换为 jquery 对象

2.在 jqvariable 上应用模糊事件,而不是在 window

3.this.closeonblur 事件的定义中已经足够好了

 var win = window.open("URL");
 var jqwin = $(win);
  $(jqwin).blur(function() {
    this.close();
 });

Little corrections in sathishkumar's answer

1.we need to convert win to jquery object

2.Apply blur event on jqvariable and not on window

3.this.close is good enough in definition of onblur event

 var win = window.open("URL");
 var jqwin = $(win);
  $(jqwin).blur(function() {
    this.close();
 });
内心旳酸楚 2025-01-04 15:36:29

使用文档进行模糊事件

  var win = window.open("URL");
  $(window).blur(function() {
    win.close();
  });

Use document for blur event

  var win = window.open("URL");
  $(window).blur(function() {
    win.close();
  });
最佳男配角 2025-01-04 15:36:29

您不能将 onblur 事件附加到 BODY,但是
您可以在窗口的 Onblur 事件上附加一个函数。

<script type="text/javascript">
    function closeme()
    {  
        window.close();
    }
    window.onblur=closeme;
</script>

由于您已编辑问题,因此您可以获得更多帮助 这里

You cannot attach onblur event to BODY but
you Can attach a function on Onblur event of window.

<script type="text/javascript">
    function closeme()
    {  
        window.close();
    }
    window.onblur=closeme;
</script>

Since you have Edited your Question, You can get more help here

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