Конструктор Worker()
создает объект Worker
, который выполняет скрипт по указанному URL-адресу. Этот скрипт должен подчиняться политике одного источника (same-origin policy).
Если URL имеет недопустимый синтаксис или нарушена политика одного источника, то будет вызвано DOMException
типа SECURITY_ERR
.
Замечание: that there is a disagreement among browser manufacturers about whether a data URI is of the same origin or not. Though 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.
Синтаксис
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
: ADOMString
specifying the type of worker to create. The value can beclassic
ormodule
. If not specified, the default used isclassic
.credentials
: ADOMString
specifying the type of credentials to use for the worker. The value can beomit
,same-origin
, orinclude
. If not specified, or if type isclassic
, the default used isomit
(no credentials required).name
: ADOMString
specifying an identifying name for theDedicatedWorkerGlobalScope
representing the scope of the worker, which is mainly useful for debugging purposes.
Исключения
- A
SecurityError
is raised if the document is not allowed to start workers. - A
NetworkError
is raised if the MIME type of one of the script istext/csv
,image/*
,video/*
, oraudio/*
. It should always betext/javacript.
- A
SyntaxError
is raised if aURL 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).
Спецификации
Спецификация | Статус | Комментарии |
---|---|---|
HTML Living Standard Определение 'Worker()' в этой спецификации. |
Живой стандарт |
Совместимость с браузерами
BCD tables only load in the browser
Смотрите также
Интерфейс Worker