• Web APIs
  • caches
  • credentialless Experimental
  • devicePixelRatio
  • documentPictureInPicture Experimental
  • event Deprecated
  • external Deprecated
  • fence Experimental
  • fullScreen Non-standard
  • innerWidth
  • launchQueue Experimental
  • locationbar
  • mozInnerScreenX Non-standard
  • mozInnerScreenY Non-standard
  • navigation Experimental
  • orientation Deprecated
  • originAgentCluster Experimental
  • performance
  • screenLeft
  • scrollbars
  • scrollMaxX Non-standard
  • scrollMaxY Non-standard
  • sessionStorage
  • sharedStorage Experimental
  • status Deprecated
  • trustedTypes
  • atob()
  • back() Non-standard Deprecated
  • blur() Deprecated
  • captureEvents() Deprecated
  • clearImmediate() Non-standard Deprecated
  • confirm()
  • dump() Non-standard
  • find() Non-standard
  • forward() Non-standard Deprecated
  • getDefaultComputedStyle() Non-standard
  • getScreenDetails() Experimental
  • moveTo()
  • prompt()
  • queryLocalFonts() Experimental
  • releaseEvents() Deprecated
  • requestFileSystem() Non-standard Deprecated
  • scroll()
  • scrollByLines() Non-standard
  • scrollByPages() Non-standard
  • setImmediate() Non-standard Deprecated
  • setResizable() Non-standard Deprecated
  • showDirectoryPicker() Experimental
  • showModalDialog() Non-standard Deprecated
  • showOpenFilePicker() Experimental
  • showSaveFilePicker() Experimental
  • sizeToContent() Non-standard
  • webkitConvertPointFromNodeToPage() Non-standard Deprecated
  • webkitConvertPointFromPageToNode() Non-standard Deprecated
  • Events
    1. beforeprint
    2. cut
    3. error
    4. hashchange
    5. messageerror
    6. orientationchange Deprecated
    7. pageswap
    8. resize
    9. scrollsnapchange Experimental
    10. scrollsnapchanging Experimental
    11. unload Deprecated
    12. vrdisplayactivate Non-standard Deprecated
    13. vrdisplayconnect Non-standard Deprecated
    14. vrdisplaydeactivate Non-standard Deprecated
    15. vrdisplaydisconnect Non-standard Deprecated
    16. vrdisplaypresentchange Non-standard Deprecated
  • Inheritance
    1. Window object when a resource failed to load or couldn't be used — for example if a script has an execution error.

      This event is only generated for script errors thrown synchronously, such as during initial loading or within event handlers. If a promise was rejected (including an uncaught throw within an async function) and no rejection handlers were attached, an addEventListener(), or set an event handler property.

      js
      addEventListener("error", (event) => {});
      
      onerror = (message, source, lineno, colno, error) => {};
      

      Note: For historical reasons, onerror on Window and WorkerGlobalScope objects is the only event handler property that receives more than one argument.

      Event type

      The event object is a Event instance otherwise.

      Event ErrorEvent

      Description

      Event handler property

      For historical reasons, the onerror event handler property, on Window and WorkerGlobalScope objects only, has different behavior from other event handler properties.

      Note that this only applies to handlers assigned to onerror, not to handlers added using addEventListener().

      Cancellation

      Most event handlers assigned to event handler properties can cancel the event's default behavior by returning false from the handler:

      js
      textarea.onkeydown = () => false;
      

      However, for an event handler property to cancel the default behavior of the error event of Window, it must instead return true:

      js
      window.onerror = () => true;
      

      When canceled, the error won't appear in the console, but the current script will still stop executing.

      Arguments

      The event handler's signature is asymmetric between addEventListener() and onerror. The event handler passed to Window.addEventListener() receives a single ErrorEvent object's properties:

      message

      A string containing a human-readable error message describing the problem. Same as ErrorEvent.message.

      Note: In HTML, the <body> element attaches error event listeners to window (not the <body> element). For this event handler, the first parameter is called event, not message, although it still contains a string; that is, you would use <body onerror="console.error(event)"> to log the error message.

      source

      A string containing the URL of the script that generated the error.

      lineno

      An integer containing the line number of the script file on which the error occurred.

      colno

      An integer containing the column number of the script file on which the error occurred.

      error

      The error being thrown. Usually an Error object.

      js
      window.onerror = (a, b, c, d, e) => {
        console.log(`message: ${a}`);
        console.log(`source: ${b}`);
        console.log(`lineno: ${c}`);
        console.log(`colno: ${d}`);
        console.log(`error: ${e}`);
      
        return true;
      };
      

      Note: These parameter names are observable with an HTML event handler attribute, where the first parameter is called event instead of message.

      This special behavior only happens for the onerror event handler on window. The ErrorEvent object.

      Examples

      Live example

      HTML

      html
      <div class="controls">
        <button id="script-error" type="button">Generate script error</button>
        <img class="bad-img" />
      </div>
      
      <div class="event-log">
        <label for="eventLog">Event log:</label>
        <textarea
          readonly
          class="event-log-contents"
          rows="8"
          cols="30"
          id="eventLog"></textarea>
      </div>
      

      JavaScript

      js
      const log = document.querySelector(".event-log-contents");
      
      window.addEventListener("error", (event) => {
        log.textContent = `${log.textContent}${event.type}: ${event.message}\n`;
        console.log(event);
      });
      
      const scriptError = document.querySelector("#script-error");
      scriptError.addEventListener("click", () => {
        const badCode = "const s;";
        eval(badCode);
      });
      

      Result

      Specifications

      Specification
      HTML
      # handler-onerror

      Browser compatibility

      BCD tables only load in the browser

      See also