Skip to content

DetachedWindowAPI

David Ortner edited this page Apr 19, 2024 · 7 revisions

DetachedWindowAPI is an API for communicating with the internal Browser in a detached Window. A Window is considered detached when the instance is created directly without going through the Browser API (e.g. new Window()).

An instance of DetachedWindowAPI is available in the property Window.happyDOM.

Signature

class DetachedWindowAPI

Example 1

import { Window } from "happy-dom";

const window = new Window({ url: "https://localhost:3000" });

window.happyDOM.settings.navigation.userAgent =
	"Mozilla/5.0 (X11; Linux x64) AppleWebKit/537.36 (KHTML, like Gecko) HappyDOM/2.0.0";

await window.happyDOM.close();

Example 2

import { Window } from "happy-dom";

const window = new Window({ url: "https://localhost:3000" });

window.document.write(`
    <script>
        setTimeout(() => {
            document.body.innerHTML = "Hello World!";
        }, 10);
    </script>
`);

await window.happyDOM.waitUntilComplete();

// Outputs "Hello World!"
console.log(window.document.body.innerHTML);

await window.happyDOM.close();

Constructor

Parameter Type Description
browserFrame IBrowserFrame Browser frame.

Properties

Property Modifiers Type Description
settings readonly IBrowserSettings Browser settings. The settings can be modified runtime.
virtualConsolePrinter readonly VirtualConsolePrinter API for reading the output of the virtual console when using a VirtualConsole.

Methods

Method Return type Description
close() Promise<void> Closes the window and associated browser.
waitUntilComplete() Promise<void> Waits for all ongoing operations to complete. This includes operations done inside loaded pages, such as loading resources and executing scripts.
abort() Promise<void> Aborts all ongoing operations.
setURL() void Sets URL without navigating the browser.
setViewport() void Sets viewport.
Clone this wiki locally