Worker()
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
USVStringrepresenting 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: ADOMStringspecifying the type of worker to create. The value can beclassicormodule. If not specified, the default used isclassic. -
credentials: ADOMStringspecifying 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: ADOMStringspecifying an identifying name for theDedicatedWorkerGlobalScoperepresenting the scope of the worker, which is mainly useful for debugging purposes.
-
Exceptions
- A
SecurityErroris 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
NetworkErroris raised if the MIME type of the worker script is incorrect. It should always betext/javascript(for historical reasons other JavaScript MIME types may be accepted). - A
SyntaxErroris 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 |
|---|
| HTML Standard (HTML) # dom-worker-dev |
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 |
18 |
4 |
11 |
5 |
1.0 |
ecmascript_modules |
80 |
80 |
No |
No |
67 |
15 |
80 |
80 |
No |
57 |
15 |
13.0 |
mime_checks |
No
See Chromium bug 794548.
|
No
See Chromium bug 794548.
|
81 |
No |
No
See Chromium bug 794548.
|
? |
No
See Chromium bug 794548.
|
No
See Chromium bug 794548.
|
81 |
No
See Chromium bug 794548.
|
? |
No
See Chromium bug 794548.
|
options_name_parameter |
70 |
18 |
55 |
No |
57 |
12.1 |
No |
70 |
55 |
49 |
12.2 |
10.0 |
options_type_parameter |
80 |
80 |
No |
No |
67 |
No |
80 |
80 |
No |
57 |
No |
13.0 |
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.
© 2005–2021 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