3

I am making an Excel add-in by Excel JavaScript API. There is a button in this add-in, clicking on it launches window.open("https://localhost:3000/#/new/", "popup", "width=1000, height=1100") and popups a browser window.

The new page is built by angularjs. It can send postMessage to the add-in opener by $window.opener.postMessage(msg, $window.opener.location.href). Now, I want to add a real-time flag to the page to specify if the page is connected to its opener add-in.

For example, when the new page popups, the flag shows connected. If we close the Excel file (eg, close the Excel program in Windows, close the browser tab in Excel online), normally the popup browser window does not close systematically, but we want the flag to show disconnected.

Does anyone know if there is any close events of add-in/file/program by Excel JavaScript API. If so, we could send a message by postMessage from the add-in to the popup browser window when the event is triggered.

Otherwise, on the side of the popup browser window, what would be an efficient way to detect if its opener is still alive?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292

3 Answers3

1

Is it the case that you want the popup to stay open (and say disconnected) sometimes? Or would you prefer to have it close automatically when the host add-in closes? If you want the latter, then use the Dialog API.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32
  • Thank you, it is good to know Dialog API. I just did a test with `Office.context.ui.displayDialogAsync('https://www.google.fr', {height: 1100, width: 1000})` in an Excel addin. It seems that hiding the add-in or closing the excel tab (in excel online) does not close the dialog systematically. – SoftTimur Mar 15 '17 at 00:14
0

I have a feeling that the regular unload event should work. so inject the $window service to your controller and set the onbeforeunload to set your flag.

app.controller("MyFlagChangerController", function ($scope, $window...)){
  $window.onbeforeunload = function (event) {
  // Set flag   
  }
});

Another way of doing things would be via the sockets. When the socket stops communicating, you'd know that the connection is dead, this is a costlier solution though, and I'm guessing the simple JS unload one should work.

Mavi Domates
  • 4,262
  • 2
  • 26
  • 45
  • The controller and `$window` is on the side of the popup browser window, but the question is about how to detect the close events of add-in/file/program. The current socket is between the popup browser window and its server, it will not detect the closing of the add-in/excel file that launches the popup window. – SoftTimur Mar 25 '17 at 22:57
0

I have not found how to listen to a close event of Excel add-in/file/program, but I have found how to detect if the opener of the popup is still alive:

$window.opener !== null

which is quite easy and seems correct (so far).

Then, at least, we could use a passive way to check if the opener is still alive, eg, by a heartbeat, or when there is no return of a message sent...

SoftTimur
  • 5,630
  • 38
  • 140
  • 292