Web/API/SharedWorker/SharedWorker

From Get docs


The SharedWorker() constructor creates a SharedWorker object that executes the script at the specified URL. This script must obey the same-origin policy.

Note: there is disagreement among browser manufacturers about whether a data URI is of the same origin or not. Although 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 SharedWorker(aURL, name);
var myWorker = new SharedWorker(aURL, options);

Parameters

aURL
A DOMString representing the URL of the script the worker will execute. It must obey the same-origin policy.
name Optional
A DOMString specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.
options Optional
An object containing option properties that can set when creating the object instance. Available properties are as follows:
type
  • A DOMString specifying the type of worker to create. The value can be classic or module. If not specified, the default used is classic.
  • credentials: A DOMString specifying the type of credentials to use for the worker. The value can be omit, same-origin, or include. If not specified, or if type is classic, the default used is omit (no credentials required).
  • name: A DOMString specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

Return value

The created worker.

Exceptions

  • A SecurityError is raised if the document is not allowed to start workers, for example if the URL has an invalid syntax or if the same-origin policy is violated.
  • A NetworkError is raised if the MIME type of the worker script is incorrect. It should always be text/javascript (for historical reasons other JavaScript MIME types may be accepted).
  • A SyntaxError is raised if aURL cannot be parsed.

Examples

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

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

myWorker.port.start();

first.onchange = function() {
  myWorker.port.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}

second.onchange = function() {
  myWorker.port.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}

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

For a full example, see our Basic shared worker example ([[../../../../../../../mdn.github.io/simple-shared-worker/index|run shared worker]].)

Specifications

Specification Status Comment
HTML Living StandardThe definition of 'SharedWorker()' in that specification. Living Standard

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
SharedWorker() constructor Chrome

Full support 4

Edge

Full support 79

Firefox

Full support 29

IE

No support No

Opera

Full support 10.6

Safari

No support 5 — 6.1

WebView Android

No support No

Chrome Android

No support No

Firefox Android

Full support 33

Opera Android

No support 11 — 14

Safari iOS

No support 5.1 — 7

Samsung Internet Android

No support 4.0 — 5.0

Strict MIME type checks for shared worker scripts Chrome

?

Edge

?

Firefox

Full support 81

IE

No support No

Opera

?

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

Full support 81

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

name option Chrome

Full support Yes

Edge

Full support ≤79

Firefox

Full support 55

IE

No support No

Opera

?

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

Full support 55

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support 4.0 — 5.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown


See also