Importing scripts and libraries to your worker

Web Workers is about modularising a block of code or functionality and running it in a stand-alone environment (ie., the worker itself). Web Workers can also load external JavaScript files and libraries via the importScripts method.

The worker can access the navigator object, to identify the user agent (browser) running it. A worker can also load scripts into it, using the importScripts(...) command.

if (navigator.userAgent.test(/MSIE/)) { // UA sniffing is *bad* practice!!
self.onmessage = function(evt) {
/* ... */

importScripts('main.js', 'block1.js', 'block2.js');

Each script is processed one at a time. The script must be on the same origin as the worker—the same domain, cname, etc. The worker then synchronously loads the JavaScript into itself, returning to continue only once the script has finished processing.

loadScripts(...) loads one or more scripts in parallel, but always executes them in the order requested.
Also, loadScripts(...) executes synchronously, meaning it blocks the rest of the worker until it finishes loading and executing the scripts.

A worker may also be terminated by the code which created the worker, by calling terminate() on the worker instance.
Related Tutorial