Skip to content
Snippets Groups Projects
Select Git revision
  • ea2b52f293662a7920d5b72cfbe5dd69b44ab040
  • master default protected
  • 1.31
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
23 results

ready.mjs

Blame
  • ready.mjs 2.15 KiB
    /**
     * Copyright schukai GmbH and contributors 2023. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import { getDocument, getWindow } from "./util.mjs";
    
    export { domReady, windowReady };
    
    /**
     * This variable is a promise that is fulfilled as soon as the dom is available.
     *
     * The DOMContentLoaded event is fired when the original HTML document is fully loaded and parsed
     * without waiting for stylesheets, images, and subframes to finish loading.
     *
     * document.readyState changes to interactive
     * window's DOMContentLoaded event gets fired
     * document.readyState changes to complete
     * window's load event gets fired load
     *
     * @license AGPLv3
     * @since 1.31.0
     * @memberOf Monster.DOM
     * @summary variable to check if dom is ready
     * @type {Promise}
     * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event
     * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
     */
    const domReady = new Promise((resolve) => {
        const document = getDocument();
    
        if (document.readyState === "loading") {
            document.addEventListener("DOMContentLoaded", resolve);
        } else {
            resolve();
        }
    });
    
    /**
     * This variable is a promise that is fulfilled as soon as the windows is available.
     *
     * The load event fires when the entire page is loaded, including all dependent resources such as stylesheets,
     * assets, and images. Unlike DOMContentLoaded, which fires as soon as the DOM of the page is loaded,
     * without waiting for the resources to finish loading.
     *
     * @license AGPLv3
     * @since 1.31.0
     * @memberOf Monster.DOM
     * @summary variable to check if window is ready
     * @type {Promise}
     * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
     * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
     */
    const windowReady = new Promise((resolve) => {
        const document = getDocument();
        const window = getWindow();
    
        if (document.readyState === "complete") {
            resolve();
        } else {
            window.addEventListener("load", resolve);
        }
    });