0

I am writing an Ext JS 5 application and seem to be losing a reference to a child window that I am opening.

The following opens a new window to w3schools, as a sample. Later, when I close the window, the beforeunload event DOES NOT fire.

 this.chatPopOutWindow = window
                .open(
                    'http://www.w3schools.com',
                    'chatPopOutWindow',
                    'width=380,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
            Ext.get(me.chatPopOutWindow).on('beforeunload', function() {
              .....more code

In this code sample with a bad url (the new window opens to a 404 error), when I close the window the beforeunload event DOES fire:

this.chatPopOutWindow = window
                .open(
                    '/someBadURL',
                    'chatPopOutWindow',
                    'width=380,height=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
            Ext.get(me.chatPopOutWindow).on('beforeunload', function() {
              .....more code

Why does the beforeunload event not get triggered in the first scenario?

Jon
  • 71
  • 1
  • 6
  • I'm not sure. I searched for a reference but didn't find. So, I believe this happens because the first scenario you're opening a window with a domain different from yours. In the second-one, even it being a badUrl, it's a relative url, which means, same domain as the opener. – João Mosmann Jun 03 '15 at 19:29
  • See: http://stackoverflow.com/questions/9736564/javascript-cross-domain-window-close-event-from-parent-window – Evan Trimboli Jun 03 '15 at 20:09
  • Thanks, Evan, but it does not work even in the same domain. – Jon Jun 03 '15 at 20:41

1 Answers1

0

I should have specified that the window I want to open is not cross-domain; it is in the same domain (though not in the example above).

Anyway, I found the answer. Apparently, Ext JS 5's Ext.get(me.chatPopOutWindow).on('beforeunload', function() {... is not reliable. Instead, use me.chatPopOutWindow.onbeforeunload = function() {..., which is the JavaScript syntax for listening for a beforeunload event..

Jon
  • 71
  • 1
  • 6