无法在Firefox中运行JavaScript时调用外部文件。

huangapple go评论60阅读模式
英文:

Cannot call an external file when running JavaScript in Firefox

问题

我尝试在我的 index.html 中运行这个:

<script>
	var stockfish = new Worker('./js/stockfish.js');

	stockfish.addEventListener('message', function (e) {
	  console.log(e.data);
	});
	 
	stockfish.postMessage('uci');
</script>

但是我收到以下错误消息:

安全错误:位于 file:///D:/ChessComparator/index.html 的内容无法从 file:///D:/ChessComparator/js/stockfish.js 加载数据

我无法在Chrome上运行Web Workers,也想不到其他浏览器来测试这个。

英文:

I'm trying to run this in my index.html:

&lt;script&gt;
var stockfish = new Worker(&#39;./js/stockfish.js&#39;);

stockfish.addEventListener(&#39;message&#39;, function (e) {
  console.log(e.data);
});
 
stockfish.postMessage(&#39;uci&#39;);
&lt;/script&gt;

But I get the following error:

Security error: content located at file:///D:/ChessComparator/index.html cannot load data from file:///D:/ChessComparator/js/stockfish.js.

I cannot run web workers on Chrome and I can't think of another browser to test this on.

答案1

得分: 1

最简单的解决这些问题的方法是避免在文件URI和其他null来源上进行测试。

如果你只是想让它工作,尝试从源目录运行python -m http.serverpython3 -m http.server来提供文件。然后,你可以在浏览器中访问http://localhost:8080以在本地查看你的网页。

英文:

The easiest solution to these sorts of problems is to avoid testing in file URI and other null origins.

If you just to get it working, try running python -m http.server or python3 -m http.server from the source directory to serve the files. Then you can visit http://localhost:8080 in your browser to view your webpage locally.

答案2

得分: -2

我发现默认情况下在Chrome本地上无法正常工作。如何在Chrome上使其工作,我不知道,但你可以使用Safari或Firefox。

英文:

i found that it just doesn't work in chrome locally by default. how to get it to work on chrome i don't know, but you can just use safari or firefox

huangapple
  • 本文由 发表于 2023年5月22日 02:47:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定