Make WordPress Core

Opened 18 months ago

Last modified 11 months ago

#58145 new defect (bug)

JS error: Uncaught ReferenceError: Element is not defined

Reported by: jankyz's profile jankyz Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.2
Component: Editor Keywords: needs-patch
Focuses: javascript Cc:

Description

I've got a console error on editor level

uncaught ReferenceError: Element is not defined

file: wp-polyfill-inert.js
line:30

(function () {
    // Return early if we're not running inside of the browser.
    if (typeof window === 'undefined') {
      return;
    }

    // Convenience function for converting NodeLists.
    /** @type {typeof Array.prototype.slice} */
    var slice = Array.prototype.slice;

    /**
     * IE has a non-standard name for "matches".
     * @type {typeof Element.prototype.matches}
     */
    var matches = Element.prototype.matches || Element.prototype.msMatchesSelector;

suggestion

(function () {
    // Return early if we're not running inside of the browser.
    if (typeof window === 'undefined' || window?.Element) {
      return;
    }

    // Convenience function for converting NodeLists.
    /** @type {typeof Array.prototype.slice} */
    var slice = Array.prototype.slice;

    /**
     * IE has a non-standard name for "matches".
     * @type {typeof Element.prototype.matches}
     */
    var matches = Element.prototype.matches || Element.prototype.msMatchesSelector;

Change History (2)

#1 @jankyz
18 months ago

  • Keywords needs-patch added
  • Summary changed from JS error: caught ReferenceError: Element is not defined to JS error: Uncaught ReferenceError: Element is not defined

#2 @mattjones2207
11 months ago

I'm also getting this.

In my case it's caused by a WebWorker within Yoast, which overwrites the window variable.

The fix would seem to be just to bail early if window is a WebWorker object, similar to the existing 'Running in browser' check.

<?php
// Return early if we're not running inside of the browser.
if (typeof window === 'undefined') {
    return;
}

// Return early if running as a webworker
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
    return;
}
Note: See TracTickets for help on using tickets.