I'm creating a single sign-on service for the company I'm working for. It has to have it's own JavaScript library like the one Facebook supplies for their oAuth service. I'm having trouble detecting when the window has closed so that I can implement failure handlers.
I have tried using window.onunload
but the moment you login the document unloads and then the window.onunload
fires. I was trying to find a way to check if there is a way of checking if the window
is in a closing state. Didn't have too much luck finding it though.
After that I tried checking if the window
was already closed through the use of window.CLOSED
. That didn't work because the window.onunload
event fires before the window has been closed.
I looked into the window
object on the Mozilla Developer Network wiki and found the window.close
event. I tried it but it doesn't fire for some reason.
The code I have implemented so far:
PopUp - Login:
window.onunload = function (e) {
try {
window.opener.postMessage([false], '*');
} catch (e) {}
};
PopUp - Login Success:
window.opener.postMessage([true, 'data' ], '*');
window.close();
Opener:
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent, function (e) {
if (e.data[0]) {
this._data = JSON.parse(e.data[1]);
return _fireEvent('success', e.data[1]);
} else {
if (win.CLOSED)
return _fireEvent('failure');
}
}, false);