window .postMessage is not returning when popup window is closed,
11:31 12 Nov 2025

I am opening a window popup for login and once login is completed I am closing the popup window. after closing. I want to know what is the exact method to capture the closing the popup window , (both cases : if closed manually and closed programmatically)

I tried below :

in parent window:

                const width = 600;
                const height = 700;
                const left = window.screenX + (window.outerWidth - width) / 2;
                const top = window.screenY + (window.outerHeight - height) / 2;
                const url = `${window.location.origin}/auth-onedrive`;
                const authWindow = window.open(
                    url,
                    "OneDrive Login",
                    `width=${width},height=${height},left=${left},top=${top}`
                );

                if (!authWindow) {
                    console.warn("Popup blocked");
                    retryLogin();
                    return;
                }

                const handleMessage = (event) => {
                    // Make sure it’s from our origin
                
                    if (event.data?.type === "onedrive-auth-result") {
                        console.log("Received message from popup:", event.data.status);
                        window.removeEventListener("message", handleMessage);
                        retryLogin();
                    }
                };

                window.addEventListener("message", handleMessage);

Pop window:

 dispatchRedux(oneDriveLogin(payload, "Authenticating with OneDrive..."))
                .then(handleResponse)
                .catch(handleError);
    
const notifyParent = (status) => {
            if (window.opener) {
                window.opener.postMessage(
                    { type: "onedrive-auth-result", status }
                );
            }
        };

        const handleResponse = (res) => {
            if (res?.status === 'success' && res?.data?.loggedIn) {
                setLoginStatus("Login Successful.");
                notifyParent("success");
                window.close();
            } else if (res?.status === 'success' && !res?.data?.loggedIn) {
                setLoginStatus("Redirecting...");
                window.location.href = res?.data?.redirectUrl;
            }
        };
    ```
   are there any other method to capture closing of the popup window in the parent ?

    
javascript browser popup window