JavaScript HTML5 Web Workers work in Firefox 4, but not in Chrome 12.0.742.122

When I tried to play around with Web Workers feature in HTML5, my firefox works happily but chrome complains that:

Uncaught TypeError: Cannot call method 'postMessage' of undefined xstartWorkerworker.html:7 (anonymous function)worker.html:1 onclickworker.html:2

worker.html

<button onclick="xstartWorker()">Start worker</button>
<output id="result"></output>
<script>
function xstartWorker()
{
  worker.postMessage({'cmd': 'startWorker', 'msg': 'Start now!'});
}

var worker = new Worker('worker.js');

worker.addEventListener('message', function(e)
  {
      document.getElementById('result').textContent = e.data;
  }
  , false);
</script>

worker.js

self.addEventListener('message', function(e)
{
  var data = e.data;
  switch (data.cmd)
  {
    case 'startWorker':
      self.postMessage('worker thread start now:' + data.msg);
      break;
    default:
      self.postMessage('default');
  }
}
, false);

What I can do to make it works in chrome?

BTW, when I tried out the sample at http://playground.html5rocks.com/#inline_workers and this time chrome works, but firefox complains that

Error: worker is undefined Source File: http://playground.html5rocks.com/ Line: 39

Answer:1

I'm guessing you're trying to run this on your local machine, not on a webserver. Workers are restricted by the Same Origin Policy, but as the linked Wikipedia page notes,

The behavior of same-origin checks and related mechanisms is not well-defined in a number of corner cases, such as for protocols that do not have a clearly defined host name or port associated with their URLs (file:, data:, etc.).

Loading a local file, even with a relative URL, is the same as loading a file with the file: protocol. So my guess is that the problem is that you're trying to load worker.js as a local file - Chrome doesn't like this (for some good security reasons), though you can force the issue by starting Chrome like this: chrome.exe --allow-file-access-from-files

Alternatively, try serving your script on a local or remote webserver and see if that fixes the problem. (If you have Python installed, you can go to the directory in question and run python -m SimpleHTTPServer 8000, then go to http://localhost:8000/ in your browser).

Answer:2

I need some help with translating the following CouchDB views from javascript to erlang. I need them in erlang, because in javascript the view uses all of the available stack memory and crashes ...

I need some help with translating the following CouchDB views from javascript to erlang. I need them in erlang, because in javascript the view uses all of the available stack memory and crashes ...

While I was debugging an error in Google Plus (while importing FB contacts from Yahoo) I've discovered strange JSON response: )]}' [[["er",,,,,500] ,["e",2,,,57] ],'45932b7d6d6dc08e'] Is it some ...

While I was debugging an error in Google Plus (while importing FB contacts from Yahoo) I've discovered strange JSON response: )]}' [[["er",,,,,500] ,["e",2,,,57] ],'45932b7d6d6dc08e'] Is it some ...

Is there a way that I can pass a JSP variable value to an external Javascript file by using only Javascript and JSP. Something like: JSP String str = "Hello"; external.js //not working var str = "&...

Is there a way that I can pass a JSP variable value to an external Javascript file by using only Javascript and JSP. Something like: JSP String str = "Hello"; external.js //not working var str = "&...

  1. pass jsp value to external javascript file

I defined a global Javascript function: function resizeDashBoardGridTable(gridID){ var table = document.getElementById('treegrid_'+gridID); ..... } After this function was used a few ...

I defined a global Javascript function: function resizeDashBoardGridTable(gridID){ var table = document.getElementById('treegrid_'+gridID); ..... } After this function was used a few ...