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

If the URL has an invalid syntax or if the same-origin policy is violated a DOMException of type SECURITY_ERR is thrown.

Note: that there is a disagreement among browser manufacturers about whether a data URI is of the same origin or not. Though Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) and later accept data URIs, that's not the case in all other browsers.

Syntax

var myWorker = new Worker(aURL, options);

Parameters

aURL
A USVString 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 DOMString specifying the type of worker to create. The value can be classic or module. If not specified, the default used is classic.
  • credentials: A DOMString 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 DOMString specifying an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

Exceptions

  • A SecurityError is raised if the document is not allowed to start workers.
  • A NetworkError is raised if the MIME type of one of the script is text/csv, image/*, video/*, or audio/*. It should always be text/javacript.
  • A SyntaxError is raised if aURL cannot be parsed.

Example

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

var myWorker = new Worker('worker.js');

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

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

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'Worker()' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 4Edge Full support YesFirefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes
Constructor name optionChrome Full support 70Edge Full support 18Firefox Full support 55IE No support NoOpera Full support 57Safari No support No
Notes
No support No
Notes
Notes Supported in Safari Technology Preview 64
WebView Android No support NoChrome Android Full support 70Edge Mobile ? Firefox Android Full support 55Opera Android Full support 57Safari iOS No support No
Notes
No support No
Notes
Notes Supported in Safari Technology Preview 64
Samsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

See also

The Worker interface it belongs to.

Document Tags and Contributors

Last updated by: ExE-Boss,