Skip to content Skip to sidebar Skip to footer

Error Trying To Instantiate Web Worker Object

Trying out the web worker API for the first time and can't seem to get a response from the background worker. Markup:

Solution 1:

The path of the file containing the worker onmessage declaration is relative to the html file - not the calling script file.

I got it working by making the following edit:

var myWorker = new Worker('../Scripts/worker.js');

Solution 2:

  1. The onmessage event pertains to the current window. So in script.js it should be

    self.onmessage = function (e)

instead of

myWorker.onmessage = function (e)
  1. The postMessage method pertains to the target window. So in worker.js it should be

    onmessage = function (e) { e.source.postMessage('OK'); }

instead of

onmessage = function (e) {
   postMessage('OK'); }

Post a Comment for " Error Trying To Instantiate Web Worker Object"