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 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
: ADOMString
specifying the type of worker to create. The value can beclassic
ormodule
. If not specified, the default used isclassic
.credentials
: ADOMString
specifying the type of credentials to use for the worker. The value can beomit
,same-origin
, orinclude
. If not specified, or if type isclassic
, the default used isomit
(no credentials required).name
: ADOMString
specifying an identifying name for theDedicatedWorkerGlobalScope
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, e.g. if the URL has an invalid syntax or if the same-origin policy is violated. - A
NetworkError
is raised if the MIME type of the worker script is incorrect. It should always betext/javascript
. - A
SyntaxError
is raised 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:
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 our Basic dedicated worker example (run dedicated worker).
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Worker()' in that specification. |
Living Standard |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Worker() constructor | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support 10 | Opera Full support 10.6 | Safari Full support 4 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 5.1 | Samsung Internet Android Full support 1.0 |
Support for ECMAScript modules | Chrome Full support 80 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support 67 | Safari No support No | WebView Android Full support 80 | Chrome Android Full support 80 | Firefox Android No support No | Opera Android Full support 57 | Safari iOS No support No | Samsung Internet Android No support No |
Strict MIME type checks for worker scripts | Chrome ? | Edge ? | Firefox Full support 70 | IE No support No | Opera ? | Safari ? | WebView Android ? | Chrome Android ? | Firefox Android No support No | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
Constructor name option | Chrome Full support 70 | Edge Full support 18 | Firefox Full support 55 | IE No support No | Opera Full support 57 | Safari
No support
No
| WebView Android No support No | Chrome Android Full support 70 | Firefox Android Full support 55 | Opera Android Full support 49 | Safari iOS
No support
No
| Samsung Internet Android Full support 10.0 |
Constructor type option | Chrome Full support 80 | Edge Full support 80 | Firefox No support No | IE No support No | Opera Full support 67 | Safari No support No | WebView Android Full support 80 | Chrome Android Full support 80 | Firefox Android No support No | Opera Android Full support 57 | Safari iOS No support No | 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.
Note: A browser can be marked as providing full support for Worker()
even though it does not support worker scripts written as modules. As of Mar 1, 2019, only Chrome 80+ supports this feature, while Firefox has an open feature request. No other browsers are known to have support for production usage of worker scripts written as modules. Without that support, worker scripts written as modules and modules used by worker scripts have to be transpiled or otherwise converted to non-module code in order to run.
See also
The Worker
interface it belongs to.