@vangware/window-open-promise

Promised Window.open


Keywords
front, front-end, javascript, open, promise, promised, window, js, promises, ts, typescript, util
License
MIT
Install
npm install @vangware/window-open-promise@2.8.2

Documentation

@vangware/window-open-promise

NPM Version Build Status

Promised Window.open.

Usage

import windowOpenPromise from "@vangware/window-open-promise";

windowOpenPromise({
    url: "https://example.com", // Optional, you might want to open an empty window
    features: { alwaysOnTop: true }
})
    .then(newWindow => {
        newWindow.console.log("This will log in the new window.");
        newWindow.addEventListener("beforeunload", _event => {
            console.log("This will log when the new window is closed.");
        });
    })
    .catch(_error => {
        console.error("This will log if the new window can't be opened.");
    });

Or if you feel you need more sugar:

import windowOpenPromise from "@vangware/window-open-promise";

try {
    const newWindow = await windowOpenPromise({
        url: "https://example.com", // Optional, you might want to open an empty window
        features: { alwaysOnTop: true }
    })
    newWindow.console.log("This will log in the new window.");
    newWindow.addEventListener("beforeunload", _event => {
      console.log("This will log when the new window is closed.");
    });
} catch (_error) {
    console.error("This will log if the new window can't be opened.");
}