W3cubDocs

/Web APIs

Worker: Worker() constructor

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

Note: that 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(aURL)
new Worker(aURL, options)

Parameters

aURL

A string representing the URL of the script the worker will execute. It must obey the same-origin policy.

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 omit (no credentials required).

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");

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

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

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Worker 4 12 3.5 10 10.6 4 4.4 18 4 11 5 1.0
ecmascript_modules 80 80 114 No 67
15["Nested workers support was introduced in Safari 15.5.", "Script loading in nested workers was introduced in Safari 16.4."]
80 80 114 57
15["Nested workers support was introduced in Safari 15.5.", "Script loading in nested workers was introduced in Safari 16.4."]
13.0
mime_checks No No 81 No No 16 No No 81 No 16 No
options_name_parameter 70 18 55 No 57 12.1 No 70 55 49 12.2 10.0
options_type_parameter 80 80 114 No 67 No 80 80 114 57 No 13.0

See also

The Worker interface it belongs to.

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker