Uncaught DOMException: Failed to construct ‘Worker’: Script at ‘file:///E:/work1.js’ cannot be accessed from origin ‘null’.
通过file://访问时,origin为 ‘null’, 但是好像通过file://访问也没有什么安全问题啊,为何Chrome不支持通过file://使用Web Worker?
浏览器不知道打开的文件是远程的还是本地的,他只知道协议是 http 还是 https。
如果你细心了,就会发现,file 协议是 3 个斜杠,而 http 以及其它协议都是 2 个斜杠。这就是 cannot be accessed from origin ‘null’. 的原因。
在 http://localhost/work1.js 中,http 是协议,localhost 是 host,/work1.js 是 path。
类比一下 file:///E:/work1.js,正确的短句是 file:// /E:/work1.js,其中 file 是协议,host 为空字符串,/E:/work1.js 是 path。
试试通过 chrome.exe --allow-file-access-from-files 参数启动 chrome。或者安装一个跨域的 chrome 扩展。
@chapgaga 别做这种假设。如果你本地开个 nginx 服务器,那么通过 http 访问的也是你的本地文件,但是对于浏览器来说,这种访问不是本地的。同理 file:// 协议虽然访问的确实是本地的文件,但是浏览器不能做这种假设。况且,file:// 也可以访问非本地文件的,是使用过网上邻居的共享文件功能吗?