Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (48 sloc) 5.11 KB



I have removed the need to manually load jQuery. In order to ensure compatibility with older browsers, jQuery is still required for ajax, but is not loaded on demand from the google CDN. jQuery is now only loaded if 1) It is not already loaded somewhere in your script and 2) web workers are not supported in the browser.


There are a few things to note before using this script:

  • This script is a work in progress and should be considered to be in a beta state
  • This script will not allow multi threading, but will allow web workers, that would otherwise not work in older browsers, to work in synchronously within your scrips.
  • This script may make your JavaScript less secure.
  • Although this script will work with most web workers, it will not work with all web workers.
  • Some web workers will need to be modified to work with this script. NOTE: This will not break the functionality of the web workers when not using this script.

So... why use this script?

Web workers are one of the most (if not the most) powerful features in HTML 5, however their adoption will be very slow because, for a long time, the majority of browsers will not support them. This forces anyone wishing to use web workers to also duplicate their code for situations where web workers are not supported, a task that is both laborious and (in the context of a company) expensive. This script removes code duplication and allows developers to use web workers without having to worry about browser compatibility. Although only browsers that are support web workers will run the script in a multi thread environment, the web workers will still work on all non supported browsers, albeit at a 'synchronous' cost.

However, not all web workers need to be computationally intensive. My wiki parsing script (see examples) makes good use of web workers, but also performs very well in browsers in which web workers are not suppored.

In a sense, this script encourages the adoption of web workers.

Advantages and Disadvantages of using this script


  • You web workers will be forwards compatible. A web worker that works with this script will also work natively in an HTML 5 compliant browser.
  • You will not have to duplicate the functionality of you web workers to allow for non HTML 5 compatible browsers.
  • There is almost no installation necessary, simply include the GWorker.js script and you can use web workers as you would in any HTML 5 compliant browser.

Disadvantages / Limitations

  • This is essentially a hack
  • Not all web workers will work with this script. This is usually due to one of two things. 1. Web workers using other HTML 5 features, such as a canvas. 2. Problems with duplicate functions (this is because some of the web workers functions will get exposed into the global namespace and could clash with other existing functions).
  • Some web workers will require some modification to work, namely declaring the onmessage and postMessage in the self namespace. This will not break compatibility when not using this script.
  • Your web workers will be run synchronously meaning that your scripts will hang until the web worker has finished executing.


Installation is simple. Simply download the gworker.js file add the following line to the head tag of any pages in which your use web workers.

<script src="gworker.js" type="text/javascript"></script>

The script will automatically detect if web workers are supported and only invoke itself if they are not.

Make sure you change the paths so that they point to the correct locations.


In order for this script to function, you must declare the onmessage and postMessage functions in your web worker in the self namespace. This is because the script substitutes the self namespace for a custom variable before loading web workers, allowing each worker to run in its own namespace. NOTE: Some web workers declare these functions in the self namespace already, some dont. This will not affect the functionality of your web worker in HTML 5 compliant browsers.

self.onmessage = function(event) {
 //.... worker code

See the examples for more information.


All the examples are contained in the examples directory. Simply open up the .html file in your favourite browser (or a non HTML 5 compliant one) and play around with the examples.

The examples are also available to view at the following URLs:

Running the tests

The tests use the Yahoo UI Test suite. The dependencies are loaded from the Yahoo servers and so you will need an internet connection to run the tests. To run the test suite just open up test-suite.html in your browser.


Can I contribute?

I welcome all contributions, simply fork the github repository, make your changes, and issue a pull request.

I have found a bug

Either log it as an issue in the issue tracker on github or send me and email at