Our volunteers haven't translated this article into বাংলা (বাংলাদেশ) yet. Join us and help get the job done!
You can also read the article in English (US).

The Worker interface of the Web Workers API represents a background task that can be easily created and can send messages back to its creator. Creating a worker is as simple as calling the Worker() constructor and specifying a script to be run in the worker thread.

Workers may in turn spawn new workers as long as those workers are hosted within the same origin as the parent page (Note: nested workers are currently not implemented in WebKit).  In addition workers may use XMLHttpRequest for network I/O, with the stipulation that the responseXML and channel attributes on XMLHttpRequest always return null.

Not all interfaces and functions are available to the script associated with a Worker.

In Firefox, if you want to use workers in extensions and would like to have access to js-ctypes, you should use the ChromeWorker object instead.

Constructors

Worker()
Creates a dedicated web worker that executes the script at the specified URL. Workers can also be constructed using Blobs.

Properties

Inherits properties from its parent, EventTarget, and implements properties from AbstractWorker.

Event handlers

AbstractWorker.onerror
An EventListener called whenever an ErrorEvent of type error bubbles through to the worker. This is inherited from AbstractWorker.
Worker.onmessage
An EventListener called whenever a MessageEvent of type message bubbles through the worker — i.e. when a message is sent to the parent document from the worker via DedicatedWorkerGlobalScope.postMessage. The message is stored in the event's data property.
Worker.onmessageerror
Is an EventHandler representing the code to be called when the messageerror event is raised.

Methods

Inherits methods from its parent, EventTarget, and implements methods from AbstractWorker.

Worker.postMessage()
Sends a message — which can consist of any JavaScript object — to the worker's inner scope.
Worker.terminate()
Immediately terminates the worker. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once. ServiceWorker instances do not support this method.

Example

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

var myWorker = new Worker('worker.js');
var first = document.querySelector('#number1');
var second = document.querySelector('#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 (run dedicated worker).

Specifications

Specification Status Comment
HTML Living Standard
The 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.

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
Worker() constructorChrome 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
onmessageChrome Full support 4Edge Full support 12Firefox 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
onmessageerrorChrome Full support 60Edge ? Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Edge Mobile ? Firefox Android Full support 57Opera Android Full support 47Safari iOS ? Samsung Internet Android No support No
postMessageChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 10
Notes
Full support 10
Notes
Notes Internet Explorer does not support Transferable objects.
Opera Full support 47Safari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support 47Safari iOS Full support YesSamsung Internet Android Full support Yes
terminateChrome Full support 4Edge Full support 12Firefox 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

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 earlier browser versions, trying to load a cross-origin worker script threw a SecurityError; in newer browsers an error event is thrown instead due to a spec change. Find out more information on 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

ডকুমেন্ট ট্যাগ এবং অবদানকারী

সর্বশেষ হালনাগাদ করেছেন: joerlo,