HTML 5 Web Worker 示例在 8.0.552.231 中不起作用

发布于 2024-10-10 02:08:08 字数 1292 浏览 4 评论 0原文

我正在关注此示例: http://www.whatwg.org/ specs/web-workers/current-work/

page.html

<!DOCTYPE HTML>
<html>
 <head>
  <title>Worker example: One-core computation</title>
 </head>
 <body>
  <p>The highest prime number discovered so far is: <output id="result"></output></p>
  <script>
   var worker = new Worker('worker.js');
   worker.onmessage = function (event) {
     document.getElementById('result').textContent = event.data;
   };
  </script>
 </body>
</html>

worker.js

var n = 1;
search: while (true) {
  n += 1;
  for (var i = 2; i <= Math.sqrt(n); i += 1)
    if (n % i == 0)
     continue search;
  // found a prime!
  postMessage(n);
}

此示例在 Mac OSX 上的 Firefox 和 Safari 版本 5.0.2 (6533.18.5) 中运行良好,但在 Chrome 中不起作用。当我调试它时,worker.js 甚至没有被列为源之一。奇怪的是,同一网站上的示例页面链接在 Chrome 中运行良好,这与我的本地代码相同。但我的本地代码在 chrome 中不起作用。

当我尝试像这样在 Javascript 调试器中手动运行代码时,

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

我收到一条错误消息:

Error: SECURITY_ERR: DOM Exception 18

还有其他人有这种经历吗?有人能提出解决方案吗?

谢谢

I'm following this example at: http://www.whatwg.org/specs/web-workers/current-work/

page.html

<!DOCTYPE HTML>
<html>
 <head>
  <title>Worker example: One-core computation</title>
 </head>
 <body>
  <p>The highest prime number discovered so far is: <output id="result"></output></p>
  <script>
   var worker = new Worker('worker.js');
   worker.onmessage = function (event) {
     document.getElementById('result').textContent = event.data;
   };
  </script>
 </body>
</html>

worker.js

var n = 1;
search: while (true) {
  n += 1;
  for (var i = 2; i <= Math.sqrt(n); i += 1)
    if (n % i == 0)
     continue search;
  // found a prime!
  postMessage(n);
}

This example works fine in firefox and Safari Version 5.0.2 (6533.18.5) on Mac OSX but doesn't work in chrome. When I debug it, worker.js is not even listed as one of the sources. What is bizarre is that the example page link on the same website works fine in chrome, which is the same code as my local code. But my local code doesn't work in chrome.

When I try to manually run code in Javascript debugger like this

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

I get an error saying:

Error: SECURITY_ERR: DOM Exception 18

Did anyone else have this experience? Can anyone suggest a solution?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2024-10-17 02:08:08

您是通过 file:/// 协议还是通过 http:// 查看此文件?您必须提供该页面,以便安全性正确处理它。

当我尝试设置 cookie 时出现未捕获错误:SECURITY_ERR:DOM 异常 18

Are you viewing this file in the file:/// protocol or over http://? You'll have to serve the page in order for security to process it correctly.

Uncaught Error: SECURITY_ERR: DOM Exception 18 when I try to set a cookie

咿呀咿呀哟 2024-10-17 02:08:08

rxgx 是正确的,我经常看到这个错误。至于解决方案,要么购买一些便宜的共享主机进行开发,要么在您自己的计算机上运行 Web 服务器。对于 Windows,下载并安装 Apache 基金会提供的 Apache 安装程序,然后按照说明进行操作。对于 Mac OS X,只需在“系统偏好设置”的“共享”部分中启用“Web 共享”即可。对于 Linux,通过包管理器安装 apache 或 lighttpd 包。

rxgx is spot on, I've seen this error often. As for a solution, either purchase some cheap shared hosting for development, or, run a web server off your own machine. For Windows, download and install the Apache installer made available from the Apache foundation, and follow the instructions. For Mac OS X, just enable Web Sharing in the Sharing section of System Preferences. For Linux, install an apache or lighttpd package through your package manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文