The Worker
interface of the Web Workers API represents a background task that can be created via script, which can send messages back to its creator. Creating a worker is done by calling the Worker("path/to/worker/script")
constructor.
Workers may themselves spawn new workers, as long as those workers are hosted at the same origin as the parent page. (Note: nested workers are not yet implemented in WebKit).
Not all interfaces and functions are available to scripts inside a Worker
. Workers may use XMLHttpRequest
for network communication, but its responseXML
and channel
attributes are always null
. (fetch
is also available, with no such restrictions.)
Constructors
Worker()
- Creates a dedicated web worker that executes the script at the specified URL. This also works for Blob URLs.
Properties
Inherits properties from its parent, EventTarget
, and implements properties from AbstractWorker
.
Event handlers
AbstractWorker.onerror
- An
EventListener
called whenever anErrorEvent
of typeerror
bubbles through to the worker. This is inherited fromAbstractWorker
. Worker.onmessage
- An
EventListener
called whenever aMessageEvent
of typemessage
bubbles through the worker — i.e. when a message is sent to the parent document from the worker viaDedicatedWorkerGlobalScope.postMessage
. The message is stored in the event'sdata
property. Worker.onmessageerror
- Is an
EventHandler
representing the code to be called when themessageerror
event is raised.
Methods
Inherits methods from its parent, EventTarget
, and implements methods from AbstractWorker
.
Worker.postMessage()
- Sends a message — consisting of any JavaScript object — to the worker's inner scope.
Worker.terminate()
- Immediately terminates the worker. This does not let worker finish its operations; it is halted at once.
ServiceWorker
instances do not support this method.
Events
message
- Fires when the worker's parent receives a message from that worker. Also available via the
onmessage
property. messageerror
- Fires when a
Worker
object receives a message that can't be deserialized. Also available via theonmessageerror
property.
rejectionhandled
- Fires every time a
Promise
rejects, regardless of whether or not there is a handler to catch the rejection. Also available through theonrejectionhandled
event handler property. unhandledrejection
- Fires when a
Promise
rejects with no handler to catch the rejection. Also available using theonunhandledrejection
event handler property.
Example
The following code snippet creates a Worker
object using the Worker()
constructor, then uses the worker object:
var myWorker = new Worker('/worker.js');
var first = document.querySelector('input#number1');
var second = document.querySelector('input#number2');
first.onchange = function() {
myWorker.postMessage([first.value, second.value]);
console.log('Message posted to worker');
}
For a full example, see ourBasic dedicated worker example ([[../../../../../../mdn.github.io/simple-web-worker/index|run dedicated worker]]).
Specifications
Specification | Status | Comment |
HTML Living StandardThe definition of 'Worker' in that specification. | Living Standard |
Browser compatibility
Support varies for different types of workers. See each worker type's page for specifics.
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Worker
|
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 |
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 |
message event
|
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.5 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
messageerror event
|
Chrome
Full support 60 |
Edge
Full support 18 |
Firefox
Full support 57 |
IE
? |
Opera
Full support 47 |
Safari
? |
WebView Android
Full support 60 |
Chrome Android
Full support 60 |
Firefox Android
Full support 57 |
Opera Android
Full support 47 |
Safari iOS
? |
Samsung Internet Android
Full support 8.0 |
onmessage
|
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 |
onmessageerror
|
Chrome
Full support 60 |
Edge
Full support 18 |
Firefox
Full support 57 |
IE
No support No |
Opera
Full support 47 |
Safari
? |
WebView Android
Full support 60 |
Chrome Android
Full support 60 |
Firefox Android
Full support 57 |
Opera Android
Full support 44 |
Safari iOS
? |
Samsung Internet Android
Full support 8.0 |
postMessage
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE Full support 10 Full support 10 Notes' Internet Explorer does not support |
Opera
Full support 47 |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support 44 |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
terminate
|
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 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.'
- See implementation notes.
Cross-origin worker error behaviour
In early versions of the spec, loading a cross-origin worker script threw a SecurityError
. Nowadays, an error
event is thrown instead. Find out how to deal with this in Loading cross-origin worker now fires error event instead of throwing; worker in sandboxed iframe no longer allowed.
See also
- Using Web Workers
- Functions and classes available to Web Workers
- Other kind of workers:
SharedWorker
and Service Worker.
Worker by Mozilla Contributors is licensed under CC-BY-SA 2.5.