I have a script which can be run either directly or, when available in the browser, as a Web Worker. I’d like to run a portion of this script only when run as a worker; so my question is, how can a script identify itself as being run this way?
I can’t see anything in the spec that would allow this to happen; am I missing something obvious?
In the following :
worker.js is invoked both as a script and as a worker.
worker.js contains :
In the worker environment, the postMessage succeeds, in the script environment it fails because either it is undefined or, in a browser, it requires a second argument.
Output is :
chrome :
firefox :
opera:
All under Ubuntu.