Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

vangware/window-open-promise

Repository files navigation

This repository's code was moved to lou.codes.


Coverage License NPM Version Open Issues

πŸͺŸ Promised Window.open();.

Usage

πŸ“¦ Node

Install @vangware/window-open-promise as a dependency:

pnpm add @vangware/window-open-promise
# or
npm install @vangware/window-open-promise
# or
yarn add @vangware/window-open-promise

Import it and use it:

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

const windowOpen = windowOpenPromise(globalThis);

windowOpen({
	url: "https://example.com", // URL is not required, you can open a blank window
	top: 10,
	left: 10,
})
	.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.");
	});

πŸ¦• Deno

Import @vangware/window-open-promise using the npm: prefix, and use it directly:

import { windowOpenPromise } from "npm:@vangware/window-open-promise";

const windowOpen = windowOpenPromise(globalThis);

try {
	const newWindow = await windowOpen({
		url: "https://example.com", // URL is not required, you can open a blank window
		top: 10,
		left: 10,
	});
	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.");
}

🌎 Browser

Import @vangware/window-open-promise using esm.sh, and use it directly:

<script type="module">
	import { windowOpenPromise } from "https://esm.sh/@vangware/window-open-promise";

	const windowOpen = windowOpenPromise(globalThis);

	try {
		const newWindow = await windowOpen({
			url: "https://example.com", // URL is not required, you can open a blank window
			top: 10,
			left: 10,
		});
		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.");
	}
</script>

Useful links