Window Open Promise Reference
๐ช Promised Window.open();
.
Usage
๐ฆ Node
Install window-open-promise
as a dependency:
1pnpm add window-open-promise2# or3npm install window-open-promise4# or5yarn add window-open-promise
Import it and use it:
1import { windowOpenPromise } from "window-open-promise";2
3const windowOpen = windowOpenPromise(globalThis);4
5windowOpen({6 url: "https://example.com", // URL is not required, you can open a blank window7 top: 10,8 left: 10,9})10 .then(newWindow => {11 newWindow.console.log("This will log in the new window.");12 newWindow.addEventListener("beforeunload", _event => {13 console.log("This will log when the new window is closed.");14 });15 })16 .catch(_error => {17 console.error("This will log if the new window can't be opened.");18 });
๐ฆ Deno
Import window-open-promise
using the npm:
prefix, and use it directly:
1import { windowOpenPromise } from "npm:window-open-promise";2
3const windowOpen = windowOpenPromise(globalThis);4
5try {6 const newWindow = await windowOpen({7 url: "https://example.com", // URL is not required, you can open a blank window8 top: 10,9 left: 10,10 });11 newWindow.console.log("This will log in the new window.");12 newWindow.addEventListener("beforeunload", _event => {13 console.log("This will log when the new window is closed.");14 });15} catch (_error) {16 console.error("This will log if the new window can't be opened.");17}
๐ Browser
Import window-open-promise
using esm.sh, and use it directly:
1<script type="module">2 import { windowOpenPromise } from "https://esm.sh/window-open-promise";3
4 const windowOpen = windowOpenPromise(globalThis);5
6 try {7 const newWindow = await windowOpen({8 url: "https://example.com", // URL is not required, you can open a blank window9 top: 10,10 left: 10,11 });12 newWindow.console.log("This will log in the new window.");13 newWindow.addEventListener("beforeunload", _event => {14 console.log("This will log when the new window is closed.");15 });16 } catch (_error) {17 console.error("This will log if the new window can't be opened.");18 }19</script>
Useful links
- ๐ Documentation: TypeDoc generated documentation.
- โณ Changelog: List of changes between versions.
- โ Tests Coverage: Coveralls page with tests coverage.
Type Aliases
WindowOpenPromiseFeatures
1type WindowOpenPromiseFeatures: Omit<WindowOpenPromiseOptions, "replace" | "target" | "url">;
WindowOpenPromise features.
WindowOpenPromiseOptions
1type WindowOpenPromiseOptions: object;
WindowOpenPromise options.
Type declaration
Member | Type | Description |
---|---|---|
height | number | WindowOpenPromise height (minimum 100). |
left | number | WindowOpenPromise left position. |
menuBar | boolean | WindowOpenPromise renders the menu bar. |
noOpener | boolean | WindowOpenPromise canโt access itโs opener. |
resizable | boolean | WindowOpenPromise is resizable. |
scrollbars | boolean | WindowOpenPromise has scrollbars. |
target | string | WindowOpenPromise target. |
titleBar | boolean | WindowOpenPromise renders the title bar. |
toolBar | boolean | WindowOpenPromise renders the tool bar. |
top | number | WindowOpenPromise top position. |
url | string | WindowOpenPromise url. |
width | number | WindowOpenPromise width (minimum 100). |
Functions
featureJoin()
1function featureJoin<Item>(2 iterable: Readonly<Iterable<Item, any, any>>,3): `${string},${string}`;
Joins given array of features with the FEATURE_SEPARATOR.
Type parameters
Type parameter |
---|
Item extends Strigifiable |
Parameters
Parameter | Type |
---|---|
iterable | Readonly <Iterable <Item , any , any >> |
Returns
`${string},${string}`
Joint string.
Example
1featureJoin(["top=10", "left=10", "resizable=1"]); // "top=10,left=10,resizable=1"
featureMap()
1function featureMap(2 iterable: Readonly<Iterable<EntryOf<WindowOpenPromiseFeatures>, any, any>>,3): Readonly<IterableIterator<string, any, any>>;
Maps array of feature entries to valid values.
Parameters
Parameter | Type |
---|---|
iterable | Readonly <Iterable <EntryOf <WindowOpenPromiseFeatures >, any , any >> |
Returns
Readonly
<IterableIterator
<string
, any
, any
>>
Array of formatted features.
Example
1featureMap([2 ["top", "10"],3 ["left", "10"],4 ["resizable", true],5]); // ["top=10", "left=10", "resizable=1"]
featureParser()
1function featureParser(features?: WindowOpenPromiseFeatures): string;
Parses features object into features string.
Parameters
Parameter | Type | Description |
---|---|---|
features ? | WindowOpenPromiseFeatures | Features object. |
Returns
string
Parsed string.
Example
1featureParser({2 top: 10,3 left: 10,4 resizable: true,5}); // "top=10,left=10,resizable=1"
featureValueMapper()
1function featureValueMapper(value: unknown): number;
Takes a feature value and returns a formatted feature value.
Parameters
Parameter | Type |
---|---|
value | unknown |
Returns
number
Feature value.
Example
1featureValueMapper(true); // 12featureValueMapper(false); // 03featureValueMapper(13); // 13
windowOpenPromise()
1function windowOpenPromise(2 global: Readonly<Pick<Window, "open">>,3): (options: WindowOpenPromiseOptions) => Promise<Window>;
Promised Window.open.
Parameters
Parameter | Type |
---|---|
global | Readonly <Pick <Window , "open" >> |
Returns
Function
Curried function with window
in context.
Curried function with window
set.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
options | WindowOpenPromiseOptions | EMPTY_OBJECT | WindowOpenPromise options. |
Returns
Promise
<Window
>
Promise with new window.
Example
1const windowOpen = windowOpenPromise(window);2windowOpen({3 url: "https://example.com",4 top: 10,5 left: 10,6})7 .then(newWindow => {8 newWindow.console.log("This will log in the new window.");9 newWindow.addEventListener("beforeunload", _event => {10 console.log("This will log when the new window is closed.");11 });12 })13 .catch(_error => {14 console.error("This will log if the new window can't be opened.");15 });
Example
1const windowOpen = windowOpenPromise(window);2windowOpen({3 url: "https://example.com",4 top: 10,5 left: 10,6})7 .then(newWindow => {8 newWindow.console.log("This will log in the new window.");9 newWindow.addEventListener("beforeunload", _event => {10 console.log("This will log when the new window is closed.");11 });12 })13 .catch(_error => {14 console.error("This will log if the new window can't be opened.");15 });