Web/API/WorkerGlobalScope/importScripts

From Get docs

The importScripts() method of the WorkerGlobalScope interface synchronously imports one or more scripts into the worker's scope.

Syntax

self.importScripts('foo.js');
self.importScripts('foo.js', 'bar.js', ...);

Parameters

A comma-separated list of DOMString objects representing the scripts to be imported. These paths are relative to html document base URL.

Return value

None.

Exceptions

NetworkError
Imported scripts were not served with a valid JavaScript MIME type (i.e. text/javascript).

Example

If you had some functionality written in a separate script called foo.js that you wanted to use inside worker.js, you could import it using the following line:

importScripts('foo.js');

importScripts() and self.importScripts() are effectively equivalent — both represent importScripts() being called from inside the worker's inner scope.

Specifications

Specification Status Comment
HTML Living StandardThe definition of 'importScripts()' in that specification. Living Standard

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
importScripts Chrome

Full support 4

Edge

Full support 12

Firefox

Full support 4

IE

Full support 10

Opera

Full support 10.6

Safari

Full support 4

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

Strict MIME type checks for importScripts() Chrome

Full support 71

Edge

Full support ≤79

Firefox

Full support 67

IE

?

Opera

Full support 58

Safari

No support No

WebView Android

Full support 71

Chrome Android

Full support 71

Firefox Android

Full support 67

Opera Android

Full support 50

Safari iOS

No support No

Samsung Internet Android

Full support 10.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown


See also