Worker()
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since июль 2015 г..
* Some parts of this feature may have varying levels of support.
Конструктор Worker() создаёт объект Worker, который выполняет скрипт по указанному URL-адресу. Этот скрипт должен подчиняться политике одного источника (same-origin policy).
Если URL имеет недопустимый синтаксис или нарушена политика одного источника, то будет вызвано DOMException типа SECURITY_ERR.
Примечание: There is a disagreement among browser manufacturers about whether a data URI is of the same origin or not. Though Gecko 10.0 and later accept data URIs, that's not the case in all other browsers.
Синтаксис
var myWorker = new Worker(aURL, options);
Параметры
- aURL
-
USVString, представляющая URL-адрес скрипта который будет выполнятьсяworker. Он должен подчиняться политике одного источника. - options Необязательный
-
An object containing option properties that can be set when creating the object instance. Available properties are as follows:
type: ADOMStringspecifying the type of worker to create. The value can beclassicormodule. If not specified, the default used isclassic.credentials: ADOMStringspecifying 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).- _
name: A _DOMStringspecifying an identifying name for theDedicatedWorkerGlobalScoperepresenting the scope of the worker, which is mainly useful for debugging purposes.
Исключения
- A
SecurityErroris raised if the document is not allowed to start workers. - A
NetworkErroris raised if the MIME type of one of the script istext/csv,image/*,video/*, oraudio/*. It should always betext/javascript. - A
SyntaxErroris raised if a URL cannot be parsed.
Пример
Следующий фрагмент кода показывает создание объекта Worker с помощью конструктора Worker() и его последующее использование:
var myWorker = new Worker("worker.js");
first.onchange = function () {
myWorker.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
Больше примеров можно найти здесь: Basic dedicated worker example (run dedicated worker).
Спецификации
| Specification |
|---|
| HTML> # dom-worker-dev> |
Совместимость с браузерами
Loading…
Смотрите также
Интерфейс Worker