Reputation: 23
I'm working on this code at work. Basically, it returns a console.log whenever the popup is closed. Using .onbeforeunload
works with every browser but IE11. With IE11 it'll work sometimes and sometimes it doesn't call at all. I'm not doing anything different, just opening the popup then closing it. I've no idea how to fix this as it's temperamental. I've pasted the function code below. Thanks
function open(config, refID, cb) {
var w = window.open(
config.baseURL,
"_blank",
"toolbar,scrollbars,resizable,top=250,left=250,width=599,height=500"
);
w.onbeforeunload = function () {
if (cb) cb()
};
}
Upvotes: 0
Views: 63
Reputation: 4335
I really don't know how you managed to make your code work. It simply won't. The problem is that you can't add an event like you are trying. You need to use addEventListener
. I made a simple snippet. Works 100% of times. No temperamental code, if such thing even existed :)
function open(url, callback) {
// Open the window
var win = window.open(url, "popup", "toolbar=1, scrollbars=1, resizable=1, top=250, left=250, width=500, height=500");
// Add the event listener
win.addEventListener('beforeunload', function () {
if(callback) callback();
}, false);
}
// Opens the window, with the url and callback as arguments
open('whatever.html', function() {
console.log('hello!');
});
Remember that you need permissions on the opened window url to be able the add the event listener to it.
Hope it helps.
Upvotes: 1