我需要区分使用 X 关闭按钮的用户驱动关闭弹出窗口和通过代码关闭。
var win= window.showModelessDialog("http://localhost/test/test.aspx",'google,....);
//Some manipulations
//Manipulation ends
if(win!=null && win.open)
{
win.close();
}
现在我可以完全访问 test.aspx 和 test.aspx.cs。我在 test.aspx 页面中定义了一个 onbeforeunload 方法,无论我关闭窗口(X 关闭还是我的代码被执行),都会调用该方法基本上想区分我的 X 关闭和程序化关闭,以便我可以进行一些后端操作
最佳答案
也许是这样的:
var MyPopup = {
_win : null,
_userClosingWindow : true,
open : function() {
var _this = this;
this._win = window.open(...);
this._win.onbeforeunload = function() {
if( _this._userClosingWindow ) {
// closed by user
}
else {
// closed in code
}
};
},
close : function() {
this._userClosingWindow = false;
this._win.close();
}
};
然后您可以使用 MyPopup.open() 和 MyPopup.close() 并且仍然知道何时调用关闭函数或何时用户关闭弹出窗口。
关于javascript - 使用javascript识别弹出窗口的X关闭事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837022/