Search code examples
javascriptweb-worker

Create a Javascript Worker passing an anonymous function


In order to instantiate a new Worker, the logic is the following:

var w = new Worker("demo_workers.js");

demo_workers.js contains the definition of what the worker will do. Is it possible to avoid the creation of a new .js and pass an internal function to that constructor? Something like

var w = new Worker(
   function(){
       alert("hey!");
   };
);

Solution

  • No the specification states that you must reference a javascript source.

    http://www.w3.org/TR/workers/#worker

    "Worker(scriptURL)" is the only option specified.