docs.rodeo

MDN Web Docs mirror

Worker: Worker() constructor

{{APIRef("Web Workers API")}} {{AvailableInWorkers("window_and_worker_except_service")}} 

The Worker() constructor creates a {{domxref("Worker")}}  object that executes the script at the specified URL. This script must obey the same-origin policy.

[!NOTE] There is a disagreement among browser manufacturers about whether a data URL is of the same origin or not. Though Firefox 10 and later accept data URLs, that’s not the case in all other browsers.

Syntax

new Worker(url)
new Worker(url, options)

Parameters

Exceptions

Examples

The following code snippet shows creation of a {{domxref("Worker")}}  object using the Worker() constructor and subsequent usage of the object:

const myWorker = new Worker("worker.js");
const first = document.querySelector("input#number1");

first.onchange = () => {
  myWorker.postMessage(first.value);
  console.log("Message posted to worker");
};

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

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

The {{domxref("Worker")}}  interface it belongs to.

In this article

View on MDN