As I mentioned in Part I, the Web Worker API is available for use in Firefox 3.5+, Safari 4+ and Chrome 4+, however the implementations are inconsistant. This has been completely overlooked by every single tutorial I’ve found.
The issue revolves around the accepted argument to postMessage() (from both the main window and the worker).
The Gecko (FireFox) implementation has excellently allowed us to pass almost anything: JSON, Objects, Strings, Numbers, Booleans, Arrays, etc. through postMessage() and have them arrive on the other end as expected. In reality, postMessage() natively serializes (read: stringify) the argument before sending it to the worker and vice versa. Security risks are minimal, because all that goes through the pipes are strings, as long as the developer isn’t irresponsibly using eval() inside the worker. (Any use of eval() should be considered irresponsible).
On the other hand, the WebKit (Safari & Chrome) implementation is limited to strings. Just strings. Got it? Strings. That’s it.
In order to level the playing field, the following code would have to be used consistently throughout your application if you wanted to use something other then strings to pass messages. Take this example:
Then in the worker…