Javascript not resolving worker path relative to current script

二次信任 提交于 2019-12-08 14:36:16

问题


I have a script at http://localhost/js/foo.js which needs to spawn a Web Worker from the file http://localhost/js/fooWorker.js. I assumed I could just do something like this:

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

However, this results in a 404 error, as the browser cannot find http://localhost/fooWorker.js. I was under the impression that worker paths were resolved relative to the script spawning the worker, so shouldn't I just be able to specify the name of another .js file in the same directory without having to provide an absolute path? Am I doing something wrong?


回答1:


From http://www.w3.org/TR/workers/:

When the Worker(scriptURL) constructor is invoked, the user agent must run the following steps:

  1. Resolve the scriptURL argument relative to the entry script's base URL, when the method is invoked.



回答2:


Actually, it should be relative to the embedded document path

For example,

I have

pathDoc\docA.html
js\b.js
js\worker\c.js

then code should be

var worker = new Worker('..\js\worker\c.js')


来源:https://stackoverflow.com/questions/12417216/javascript-not-resolving-worker-path-relative-to-current-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!