Search code examples
javascriptnode.jsoffice-jspostmessageheartbeat

Close event of Excel add-in/file/program or check if the opener is still alive


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?


Solution

  • 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...