当用户关闭(弹出)窗口时调用一些 JavaScript [英] Call some JavaScript when the user closes a (popup) window

查看:28
本文介绍了当用户关闭(弹出)窗口时调用一些 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在用户关闭窗口时调用一些 JS?

Is it possible to call some JS when the user closes a window?

我搜索了 JS 事件处理程序,发现只有 onunload,它在用户离开页面时调用脚本,不一定关闭窗口.

I searched for JS event handlers and found only onunload, which calls the script whenever the user leaves a page, not necessarily closing the window.

预期用途是调用 AJAX 脚本,该脚本取消设置与弹出窗口相关的几个会话.可能有也可能没有更好的方法来实现这个目标?

The intended use is to call an AJAX script that unsets a few sessions pertinent to the popup window. There may or may not be a better way of achieving this aim?

我找到了 this DevShed thread,其中声明这可以通过 JS 实现,但并没有具体说明如何调用脚本.

I found this DevShed thread in which it is stated that this is possible through JS, but it does not explain exactly how to call the script.

不幸的是,大多数 Google 搜索的解决方案都是打开 window.close,这与我所需要的正好相反!

Unfortunately most Google searches for a solution to this bring up window.close, which is effectively the opposite to what I need!

推荐答案

以下代码适用于 Firefox、IE 8 和Google Chrome".

The following code works in Firefox, IE 8, and "Google Chrome".

在打开的窗口中

<script src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.3/mootools-yui-compressed.js"></script>
<script>
function on_popup_close(){
  //put your code here
  alert('it closed');
}
</script>

在弹出窗口中

<script>
function inform_parent(){
  opener.on_popup_close();
} 

window.onbeforeunload = inform_parent;
</script>

打开窗口代码中的第一行可以是您最喜欢的实现美元符号运算符的框架.

The first line in the code for the opening window can be your favorite framework that implements the dollar sign operator.

在这里查看我的演示

这篇关于当用户关闭(弹出)窗口时调用一些 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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