W3C home > Mailing lists > Public > whatwg@whatwg.org > September 2009

[whatwg] Using Web Workers without external files

From: Simon Pieters <simonp@opera.com>
Date: Wed, 23 Sep 2009 12:34:01 +0200
Message-ID: <op.u0pgyze9idj3kv@zcorpandell.linkoping.osa>
I'd like a way to use workers without having to use an external resource.  
This would allow easier testing, mashups, small standalone apps, and so  
forth.

Possible ways to do it:

* Support data: URLs in the constructor. A bit annoying to work with,  
though.
      new Worker('data:...');
      new SharedWorker('data:...', 'foo');
* Point to an element within the same document with a fragment identifier.
      <script type=text/x-worker id=worker>...</script>
      new Worker('#worker');
      new SharedWorker('#worker', 'foo');
* Pass a string to the constructor with an additional parameter saying  
that it's a string of script rather than a URL.
      new Worker('...', true);
      new SharedWorker('...', 'foo', true);
* Mint new constructors that you pass a string to instead of a URL.
      new WorkerXYZ('...');
      new SharedWorkerXYZ('...', 'foo');
* ...other?


Currently it seems it is possible to work around the external resource  
limitation by doing:

     <!--
     postMessage('works!');
     /*
     -->
     <!doctype html>
     <script>
     // The empty string should work too, per spec, AFAICT, but
     // Firefox and Chrome throw an exception for the empty string
     var worker = new Worker('?');
     worker.onmessage = function(e) { alert(e.data) }
     </script>
     <!--
     */
     //-->

...but this is a bit too hacky for my taste.

-- 
Simon Pieters
Opera Software
Received on Wednesday, 23 September 2009 03:34:01 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:52 UTC