简体   繁体   中英

Why HTML5 Web Worker have to be initialized within a HTML file in Chrome Browser?

I just made a script for a web-worker and wondered whether this is another bug chrome-browser suffers from. I do not know whether another chromium-bug has been fixed already because you could not initialized another worker(nested) within a running worker in chromium: https://code.google.com/p/chromium/issues/detail?id=31666

This in the main.html:

<script language="JavaScript" src="workerScript.js" type="text/javascript"></script>
<script language="JavaScript" src="workerScript2.js" type="text/javascript"></script>

<script>

    // no error is thrown when workerScript.init('workerScript2.js') is called
    console.log(workerScript.init('workerScript2.js')); // Worker { ... }

</script>

workerScript.js:

function callWorker(){
    var worker = new Worker('workerScript.js');
}

callWorker(); // Uncaught ReferenceError: Worker is not defined

workerScript2:

var workerScript = {
  init: function(file){
    var worker = new Worker(file);
    return worker;
  }
};

When main.html is executed in chromium(version 40.0.2214.91 m) an error is thrown in workerScript.js:

Uncaught ReferenceError: Worker is not defined

In chromiums it seems that as long as you initialize a worker in an executed html-file no error is thrown except within an external js-file when a function is called or new Worker(...) is executed as it is the case in workerScript.js.

Actuall question: Is this a security-issue/precaution of chromium because if no error was thrown would workerScript.js be an infinite loop in chromium?(a worker in workerScript.js tries to open workerScript.js again?) But why is this working in other browsers like Firefox or even IE?

According to the comments provided by Rob W it's the same bug described at the top of the question.

Hopefully this bug is going to be fixed anytime soon. Because I've got to check whether it's a chrome-browser and if so only running one main-worker instead of several nested-workers. Chrome is fast but in terms of computing with workers it could be made faster.

Thanks for your time.

As of 9/8/16 this has not been resolved. We recently had a few comments on the thread (link in OP), but still no traction. There is a larger issue that android/safari don't support shared workers at all. There is a nice workaround involving shuttling the worker thread data between them, but this ties up the main thread... and isn't supported (last I checked) in Firefox (woo).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM