What is wrong with this WebWorker (no errors, but console.log are not reached)
I have the following code, trying to test out WebWorkers. I have an index.html file that looks like this:
<html>
<head></head>
<body>
<script type='text/javascript'>
var worker = new Worker('./myworker.js');
console.log('after creation');
worker.addEventListener('message', function(msg){
console.log(msg);
});
worker.postMessage();
</script>
</body>
</html>
The contents of myworker.js (which resides in the same directory as index.html) is:
this.onmessage = function(){
postMessage('got the msg, thanks');
};
When I load index.html (in Chrome 14), the 'after creation' console.log never happens. Nor anything else. Console.logs happen before the new Worker() creation, but nothi开发者_开发知识库ng after seems to happen.
Well butter my biscuit, apparently WebWorkers do not work when loaded locally (e.g. from file://).
source: http://www.html5rocks.com/en/tutorials/workers/basics/ (bottom of content)
If you are testing app in chrome you should start the chrome with --allow-file-access-from-files
or test app using Local server
精彩评论