1

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);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Feanaro
  • 922
  • 3
  • 19
  • 35
  • What is `win.CLOSED`? There exists a read-only property on the return value of `window.open()` called `closed`, but this is all-lowercase. You could use setTimeout to detect when the popup is closed; when `win.closed` is `false` (where `win` is the return value of `window.open()`). – Rob W Oct 18 '13 at 13:27
  • `win.CLOSED` is indeed the property you are talking about on the `window.open()` return value. The `win` variable holds the return value from the `window.open()` call which opens the popup. Visual Studio Intellisense said the property to be all upper. So that might be one of my mistakes. – Feanaro Oct 18 '13 at 21:06
  • I thought about that option too, but to me it seems a dirty option so that's why I wanted to ask if there is a better option available. – Feanaro Oct 18 '13 at 21:12
  • Does this answer your question? [Javascript detect closing popup loaded with another domain](https://stackoverflow.com/questions/15694567/javascript-detect-closing-popup-loaded-with-another-domain) – Brian Tompsett - 汤莱恩 Aug 23 '22 at 10:37

1 Answers1

0

Javascript detect closing popup loaded with another domain is a similar question. See if the answer suggested there helps you solve the problem you have.

Community
  • 1
  • 1
Zlatin Zlatev
  • 3,034
  • 1
  • 24
  • 32
  • The primary answer to that question is based on the same thing @Rob W suggested in the comments directly to my question. – Feanaro Oct 19 '13 at 20:43