Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. WorkerGlobalScope
  4. importScripts()

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.

Syntax

js
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 atext/javascript MIME type or without one of the permittedlegacy JavaScript MIME types.

TypeError

Thrown if the currentWorkerGlobalScope 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:

js
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

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp