A
A
Andrey Dyrkov2015-09-25 18:11:59
JavaScript
Andrey Dyrkov, 2015-09-25 18:11:59

Chrome blocks worker execution, how to fix it?

There is an index.html file which is open in Chrome browser

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script>
    if(!!window.Worker){
      var worker = new Worker('task.js');
      worker.postMessage();
    }else{
      console.log('Workers in not support!');
    }
  </script>
</body>
</html>

When you open the browser, it gives out this thing
Uncaught SecurityError: Failed to construct 'Worker': Script at 'file:///I:/%7BH%7D/Andrey/LanAndDev/JavaScript/WebWorkers/task.js' cannot be accessed from origin 'null'.

I understand that this is a security bug, but how to use the worker in everyday life and wherever it is supported?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex K, 2015-09-25
@VIKINGVyksa

Deploy a local web server and the error will go away. The problem is in the file:/// protocol - workers are prohibited when executing scripts from a local file.

D
Denis Ineshin, 2015-09-25
@IonDen

Just put yourself a local server of some kind. For example:
https://github.com/gruntjs/grunt-contrib-connect
https://www.apachefriends.org/en/index.html
or something like that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question