# Intercepting beforeClose event on document/window

**URL:** <https://forums.creativeclouddeveloper.com/t/intercepting-beforeclose-event-on-document-window/11454>\
**Category:** InDesign\
**Tags:** uxp\
**Created:** [October 2, 2025, 3:26pm UTC](https://forums.creativeclouddeveloper.com/t/intercepting-beforeclose-event-on-document-window/11454 "2025-10-02T15:26:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konogan](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/konogan/32/4033_2.png) [@Konogan](https://forums.creativeclouddeveloper.com/u/Konogan)\
**Post date:** [October 2, 2025, 3:26pm UTC](https://forums.creativeclouddeveloper.com/t/intercepting-beforeclose-event-on-document-window/11454/1 "2025-10-02T15:26:52Z")

</div>

Hi, I’d like to be able to intercept the beforeClose event when closing a document, so I can run some processing before the window actually closes.

From what I’ve read and tested, the beforeClose event is not cancellable on the **document** itself, but it should be cancellable on the **window**.

However, whatever I try, the window close event always appears as invalid.

Has anyone managed to make this work, or do you have an idea of how to properly intercept it?

Thanks!

Here’s a code snippet to illustrate this point.

```javascript
const { app } = require("indesign");

function addListenerToWindow(myEvent) {
    const doc = myEvent.target;
    for (let index = 0; index < app.windows.count(); index++) {
        const window = app.windows.item(index);
        if (window.parent.id === doc.id) {
            window.addEventListener("beforeClose", beforeCloseWindow)
        }
    }
}

function beforeCloseWindow(evt) {
    console.log(evt) // => Event {isValid: false, constructorName: ""}
    console.log("beforeCloseWindow");
}

app.addEventListener("afterNew", addListenerToWindow);

```
