Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

DedicatedWorkerGlobalScope.postMessage()

Onze vrijwilligers hebben dit artikel nog niet naar het Nederlands vertaald. Doe mee en help de klus te klaren!

The postMessage() method of the DedicatedWorkerGlobalScope interface sends a message to the main thread that spawned it. This accepts a single parameter, which is the data to send to the worker. The data may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.

The main scope that spawned the worker can send back information to the thread that spawned it using the Worker.postMessage method.

Syntax

postMessage(aMessage, transferList);

Parameters

aMessage
The object to deliver to the main thread; this will be in the data field in the event delivered to the Worker.onmessage handler. This may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.
transferList Optional
An optional array of Transferable objects to transfer ownership of. If the ownership of an object is transferred, it becomes unusable (neutered) in the context it was sent from and it becomes available only to the main thread it was sent to.
Only MessagePort and ArrayBuffer objects can be transferred.

Returns

Void.

Example

The following code snippet shows worker.js, in which an onmessage handler is used to handle messages from the main script. Inside the handler a calculation is done from which a result message is created; this is then sent back to the main thread using postMessage(workerResult);

onmessage = function(e) {
  console.log('Message received from main script');
  var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
  console.log('Posting message back to main script');
  postMessage(workerResult);
}

In the main script, onmessage would have to be called on a Worker object, whereas inside the worker script you just need onmessage because the worker is effectively the global scope (DedicatedWorkerGlobalScope).

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

Note: postMessage() can only send a single object at once. As seen above, if you want to pass multiple values you can send an array.

Specifications

Specification Status Comment
WHATWG HTML Living Standard
The definition of 'DedicatedWorkerGlobalScope.postMessage()' in that specification.
Living Standard No change from Web Workers.
Web Workers
The definition of 'DedicatedWorkerGlobalScope.postMessage()' in that specification.
Candidate Recommendation Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4 3.5 (1.9.1) 10 10.6 4
Feature Android Chrome for Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? (Yes) 1.0 (1.9.1) 1.0.1 10 11.5 5.1

See also

The DedicatedWorkerGlobalScope interface it belongs to.

Documentlabels en -medewerkers

 Aan deze pagina hebben bijgedragen: teoli, kscarfone, chrisdavidmills
 Laatst bijgewerkt door: teoli,