CoolUserName
CoolUserName

Reputation: 3755

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 nothing after seems to happen.

Upvotes: 1

Views: 769

Answers (2)

CoolUserName
CoolUserName

Reputation: 3755

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)

Upvotes: 1

kongaraju
kongaraju

Reputation: 9606

If you are testing app in chrome you should start the chrome with --allow-file-access-from-files or test app using Local server

Upvotes: 0

Related Questions