title | short-title | slug | page-type | browser-compat |
---|---|---|---|---|
SharedWorker: SharedWorker() constructor |
SharedWorker() |
Web/API/SharedWorker/SharedWorker |
web-api-constructor |
api.SharedWorker.SharedWorker |
{{APIRef("Web Workers API")}}
The SharedWorker()
constructor creates a
{{domxref("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 URL is of the same origin or not. Although Firefox 10.0 and later accept data URLs, that's not the case in all other browsers.
new SharedWorker(aURL)
new SharedWorker(aURL, name)
new SharedWorker(aURL, options)
-
aURL
- : A string representing the URL of the script the worker will execute. It must obey the same-origin policy.
-
name
{{optional_inline}}- : A string specifying an identifying name for the {{domxref("SharedWorkerGlobalScope")}} representing the scope of the worker, which is useful for creating new instances of the same SharedWorker and debugging.
-
options
{{optional_inline}}-
: An object containing option properties that can set when creating the object instance. Available properties are as follows:
type
- : A string specifying the type of worker to
create. The value can be
classic
ormodule
. If not specified, the default used isclassic
.
- : A string specifying the type of worker to
create. The value can be
credentials
- : A string specifying the type of
credentials to use for the worker. The value can be
omit
,same-origin
, orinclude
. If not specified, or if type isclassic
, the default used isomit
(no credentials required).
- : A string specifying the type of
credentials to use for the worker. The value can be
name
- : A string specifying an identifying name for the {{domxref("SharedWorkerGlobalScope")}} representing the scope of the worker, which is mainly useful for debugging purposes.
sameSiteCookies
- : A string indicating which
SameSite
cookies should be available to the worker. Can have one of the following two values:- 'all'
- :
SameSite=Strict
,SameSite=Lax
, andSameSite=None
cookies will all be available to the worker. This option is only supported in first-party contexts, and is the default in first-party contexts.
- :
- 'none'
- : Only
SameSite=None
cookies will be available to the worker. This option is supported in first-party and third-party contexts, and is the default in third-party contexts.
- : Only
- 'all'
- : A string indicating which
-
SecurityError
{{domxref("DOMException")}}- : Thrown 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, or if the
sameSiteCookies
value is not supported in the given context.
- : Thrown 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, or if the
NetworkError
{{domxref("DOMException")}}- : Thrown 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).
- : Thrown if the MIME type of the worker script is incorrect. It should always be
SyntaxError
{{domxref("DOMException")}}- : Thrown if
aURL
cannot be parsed.
- : Thrown if
The following code snippet shows creation of a {{domxref("SharedWorker")}} object using
the SharedWorker()
constructor and subsequent usage of the object:
const myWorker = new SharedWorker("worker.js");
myWorker.port.start();
first.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
second.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
console.log("Message received from worker");
};
For a full example, see our Basic shared worker example (run shared worker.)
{{Specifications}}
{{Compat}}
- The {{domxref("SharedWorker")}} interface it belongs to.