This translation is incomplete. Please help translate this article from English

A interface SharedWorker representa um tipo de worker específico que pode ser acedido  a partir de vários contextos de navegação, tais como várias janelas, iframes ou até workers. Eles implementam uma interface diferente do que os workers dedicados e têm um âmbito global diferente, SharedWorkerGlobalScope.

Nota: If SharedWorker can be accessed from several browsing contexts, all those browsing contexts must share the exact same origin (same protocol, host and port).

Nota: In Firefox, shared workers cannot be shared between private (i.e. browsing in a private window) and non-private documents (see bug 1177621.)


Creates a shared web worker that executes the script at the specified URL.


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

Is an EventListener that is called whenever an ErrorEvent of type error bubbles through the worker.
SharedWorker.port Read only
Returns a MessagePort object used to communicate and control the shared worker.


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


In our Basic shared worker example (run shared worker), we have two HTML pages, each of which uses some JavaScript to perform a simple calculation. The different scripts are using the same worker file to perform the calculation — they can both access it, even if their pages are running inside different windows.

The following code snippet shows creation of a SharedWorker object using the SharedWorker() constructor. Both scripts contain this:

var myWorker = new SharedWorker('worker.js');

Both scripts then access the worker through a MessagePort object created using the SharedWorker.port property. If the onmessage event is attached using addEventListener, the port is manually started using its start() method:


When the port is started, both scripts post messages to the worker and handle messages sent from it using port.postMessage() and port.onmessage, respectively:

first.onchange = function() {
  console.log('Message posted to worker');

second.onchange = function() {
  console.log('Message posted to worker');

myWorker.port.onmessage = function(e) {
  result1.textContent =;
  console.log('Message received from worker');

Inside the worker we use the SharedWorkerGlobalScope.onconnect handler to connect to the same port discussed above. The ports associated with that worker are accessible in the connect event's ports property — we then use MessagePort start() method to start the port, and the onmessage handler to deal with messages sent from the main threads.

onconnect = function(e) {
  var port = e.ports[0];

  port.addEventListener('message', function(e) {
    var workerResult = 'Result: ' + ([0] *[1]);

  port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.

Outro exemplo: Another basic share worker example (run another shared worker.)


Especificação Estado Comentário
HTML Living Standard
The definition of 'SharedWorker' in that specification.
Living Standard No change from Unknown.

Compatibilidade de navegador

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Funcionalidade Chrome Firefox (Gecko) Internet Explorer Opera Safari
Support 4 29.0 (29.0) No support 10.60 5
No support 6.1
Constructor name option (Yes) 55 (55) ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Support No support No support 33.0 (33.0) No support 11.5 5.1
No support 7.1
Constructor name option ? (Yes) 55.0 (55) ? ? ?

Consultar também