Worker: Worker() constructor

Note: This feature is available in Web Workers, except for Service Workers.

The Worker() constructor creates a Worker object that executes the script at the specified URL. This script must obey the same-origin policy.

Note: There is a disagreement among browser manufacturers about whether a data URL is of the same origin or not. Though Firefox 10 and later accept data URLs, that's not the case in all other browsers.

Syntax

js
new Worker(url)
new Worker(url, options)

Parameters

url

A string representing the URL of the script the worker will execute. It must obey the same-origin policy. The URL is resolved relative to the current HTML page's location.

Note: Bundlers, including webpack, Vite, and Parcel, recommend passing URLs that are relative to import.meta.url to the Worker() constructor. For example:

js
const myWorker = new Worker(new URL("worker.js", import.meta.url));

This way, the path is relative to the current script instead of the current HTML page, which allows the bundler to safely do optimizations like renaming (because otherwise the worker.js URL may point to a file not controlled by the bundler, so it cannot make any assumptions).

options Optional

An object containing option properties that can be set when creating the object instance. Available properties are as follows:

type

A string specifying the type of worker to create. The value can be classic or module. If not specified, the default used is classic.

credentials

A string specifying the type of credentials to use for the worker. The value can be omit, same-origin, or include. If not specified, or if type is classic, the default used is same-origin (only include credentials for same-origin requests).

name

A string specifying an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

Exceptions

SecurityError DOMException

Thrown if the document is not allowed to start workers, e.g. if the URL has an invalid syntax or if the same-origin policy is violated.

NetworkError DOMException

Thrown if the MIME type of the worker script is incorrect. It should always be text/javascript (for historical reasons other JavaScript MIME types may be accepted).

SyntaxError DOMException

Thrown if aURL cannot be parsed.

Examples

The following code snippet shows creation of a Worker object using the Worker() constructor and subsequent usage of the object:

js
const myWorker = new Worker("worker.js");
const first = document.querySelector("input#number1");

first.onchange = () => {
  myWorker.postMessage(first.value);
  console.log("Message posted to worker");
};

For a full example, see our Basic dedicated worker example (run dedicated worker).

Specifications

Specification
HTML
# dom-worker-dev

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Worker() constructor
Support for ECMAScript modules
Strict MIME type checks for worker scripts
options.name parameter
options.type parameter

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.

See also

The Worker interface it belongs to.