Question

Internally, Firefox will JSON encode an object passed via postMessage to and from the Web Worker. However, this only works in Trunk builds of Firefox (3.6+) and not in Firefox 3.5, so the question is really how to add backwards support of this operation to the current platform. The window.atob() and window.btoa() methods had been suggested before, but alas these are not available internally to the threads because they don't have access to the DOM.

Mozilla publicly states this on their developer wiki, but it has been noticed by many in the community that this happens. Check ejohn's blog test: http://ejohn.org/files/bugs/postMessage/

I've verified that this is the case as well, in 3.5, it passes only strings, and in 3.6 is will pass the object.

Was it helpful?

Solution

I haven't noticed the automatic JSON-encoding not working in Firefox 3.5, but I've mainly been working with Gears, which doesn't support it anyway.

Try including a JSON utility in both the worker script and the parent script, then manually encode and decode it yourself. This works fairly well for me with Gears.

This approach shouldn't break when Firefox begins automatically doing the JSON encoding for you, since the encoded JSON string will remain a string.

OTHER TIPS

In JavaScript there are two functions respectively for decoding and encoding base64 strings:

atob() btoa()

I found the solution to my own problem!

It seems that if the thread variable, even if declared globally, loses its .onmessage property if that said property was declared inside another function. If the property is instantiated on the global scope, then JSON messages are parsed correctly.

I'm still not sure I understand what's going on here, but at least I've figured out a way to pass objects around without having to rely on any additional stingify/json libraries.

If anyone could explain this to me so I have a better understanding, it would be appreciated :)

I setup a test case here: http://x.miernicki.com/ which logs the inter-thread messages to the firebug console if anyone cares. This helped me to get objects passed around in Fox3.5 and ultimately allowed me to see what the problem was.

Since you are clearly looking for a FF-only solution, have you tried yourObject.toSource()?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top