我在视图内部处理了一些signalR消息。该处理程序使用window.open()打开一个新窗口。当该窗口关闭时,我需要处理一些事情。我在窗口上使用.onunload(),以便在关闭时可以处理某些事情。我注意到.onunload()在打开窗口后立即被击中,这没有任何意义,应该在关闭窗口时将其关闭。这是我正在使用的代码:
connection.on("ProcessStartEnvironmentReply", (env) =>
{
var url = env.url + '/#/?username=' + env.username + '&password=' + env.password;
environmentId = env.environmentID;
console.log("Start reply msgid: " + env.sourceMessageId + ", env: " + environmentId + " at " + new Date());
arenaWin = window.open(url, '_blank');
arenaWin.onbeforeunload = function ()
{
return "Do you really want to close?";
};
arenaWin.onunload = function ()
{
//TODO: This log message is getting hit as soon as the tab is opened, not useful until fixed.
console.log("CLOSING TAB ------------------");
//$.ajax({
// url: '/api/dashboard/stop/' + environmentId,
// type: "GET",
// dataType: "JSON",
// success: function (env) {
// $("#btnStart").attr("disabled", false);
// console.log(env);
// }
//});
};
arenaWin.addEventListener('unload', function (e)
{
var confirmationMessage = "--------------------\o/----------------------";
console.log("CLOSING TAB EVENT ADDED");
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage;
});
});
有人遇到这样的事情吗?