WorkerGlobalScope: importScripts() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is only available inWeb Workers.
TheimportScripts()
method of theWorkerGlobalScope
interface synchronously imports one or more scripts into the worker's scope.
In this article
Syntax
importScripts(path0)importScripts(path0, path1)importScripts(path0, path1, /* …, */ pathN)
Parameters
pathN
A string value representing the URL of the script to be imported. The URL may be absolute or relative. If the URL is relative, it is relative to the worker entry script's URL.
Return value
None (undefined
).
Exceptions
NetworkError
Imported scripts were served without a
text/javascript
MIME type or without one of the permittedlegacy JavaScript MIME types.TypeError
Thrown if the current
WorkerGlobalScope
is a module. Useimport
instead.
Examples
If you had some functionality written in a separate script calledfoo.js
that you wanted to use insideworker.js
, you could import it using the following line:
importScripts("foo.js");
foo.js
should be in the same URL subtree as the worker's entry point — for example, if this worker was created withnew Worker("worker.js")
, thenworker.js
is the entry point. Ifworker.js
is athttps://example.com/scripts/worker.js
, thenfoo.js
should be athttps://example.com/scripts/foo.js
.
importScripts()
andself.importScripts()
are effectively equivalent — both representimportScripts()
being called from inside the worker's inner scope.
Specifications
Specification |
---|
HTML> # dom-workerglobalscope-importscripts-dev> |
Browser compatibility
Loading…