Shared Worker Example
A web worker is a single JavaScript file loaded and executed on a separate thread (=background and not the UI thread). Dedicated web workers are linked to their owner/creator which is the script that called and loaded them. Shared web workers allow any number of scripts to communicate with a single worker.
Btw, if you missed Part1 and/or Part2 of this series, you might want to read them first.
Shared web workers are identified in two ways: either by the URL of the script used to create it or by explicit name. In the code below we will see how it can be done.
Shared web workers can:
- load further scripts with
importScripts() - attach error handlers, and
- run the
port.close()method to prevent further communication on a specific port.
Features Available to Workers
Due to their multi-threaded behavior, web workers only has access to a subset of JavaScript’s features:
- The
navigatorobject - The
locationobject (read-only) XMLHttpRequestsetTimeout()/clearTimeout()andsetInterval()/clearInterval()- The Application Cache
- Importing external scripts using the
importScripts()method - Spawning other web workers
Workers do NOT have access to
- The DOM (it’s not thread-safe)
- The
windowobject - The
documentobject - The
parentobject
Loading External Scripts
You can load external script files or libraries into a worker with the importScripts() function. The method takes zero or more strings representing the filenames for the resources to import. This example loads script1.js and script2.js into the worker: worker.js:
importScripts('script1.js');
importScripts('script2.js');
Which can also be written as a single import statement:
importScripts('script1.js', 'script2.js');
you might want to debug your worker with this pattern
var worker = new Worker("worker.js");
worker.onerror = function(e){
throw new Error(e.message + " (" + e.filename + ":" + e.lineno + ")");
};
and if you are on Chrome (17+) check this great option under the dev tools:

In case you missed Part1 and/or Part2 of this series – feel free to check them out.
More Sources
- Web Workers Examples I’ve written to my book
- whatwg.org – and the Shared Workers section
- Live example: web workers & prime numbers & messages
- Good option to debug web workers – if you didn’t notice the tip on part 2 or you are not in Chrome dev tool for some reason.

Hello,
I dont understand you properly (probably) you say, that standard workers (dedicated) created by new Workers() constructor cannot be accessed from another scripts if they arent creator of worker … It doesn seems be true, since its not important in which script it was created, but it depends on scope in which was created. It will be fantastic if you could make your thoughs clear for me. Thank you in advance.
Hey Johnny,
You are right – web workers depends on the scope.
Can you point me to where I said it?
Thanks!
I cannot help, but the beggining of article:
Dedicated web workers are linked to their owner/creator which is the script that called and loaded them. Shared web workers allow any number of scripts to communicate with a single worker.
is somehow confusing for me, after reading this article, I was curious how you though this and played around, and probably got it. Let me show, what I have found -> By your words you mean, that many scripts (for example even in another tab or so on) can create pseudo instance(s) (something like connector object(s)) of Shared Worker(s) and connect to it (to concrete instance of desired Shared Worker, by the name, or so on) where these rules apply: each instance of Shared Worker share same global context and (and there its little tricky as I am very new to Shared Workers) this instance (I assume in fact its just one instance, of one Shared Worker, there are not many of them, right? ) lives as long as at least one pseudo object (Im still not gonna call them workers as I see them just like communication objects) is alive.
Sorry for that bloody english, its not my natural language, I dont care about grammer much and my sentences are very unnatural even in my language. Hope you see what I am trying to reveal.