HTML5网络工作者在Firefox 4中工作,但不在Chrome 12.0.742.122中工作 [英] HTML5 Web Workers work in Firefox 4, but not in Chrome 12.0.742.122
问题描述
当我试图在HTML5中使用Web Workers功能时,我的firefox很开心,但是chrome抱怨:
$ b
When I tried to play around with Web Workers feature in HTML5, my firefox works happily but chrome complains that:
Uncaught TypeError:无法调用undefined
的方法'postMessage'xstartWorkerworker.html:7(匿名函数)worker.html:1
onclickworker.html:2
Uncaught TypeError: Cannot call method 'postMessage' of undefined xstartWorkerworker.html:7 (anonymous function)worker.html:1 onclickworker.html:2
worker.html
worker.html
<button onclick="xstartWorker()">Start worker</button>
<output id="result"></output>
<script>
function xstartWorker()
{
worker.postMessage({'cmd': 'startWorker', 'msg': 'Start now!'});
}
var worker = new Worker('worker.js');
worker.addEventListener('message', function(e)
{
document.getElementById('result').textContent = e.data;
}
, false);
</script>
worker.js
worker.js
self.addEventListener('message', function(e)
{
var data = e.data;
switch (data.cmd)
{
case 'startWorker':
self.postMessage('worker thread start now:' + data.msg);
break;
default:
self.postMessage('default');
}
}
, false);
我能做些什么使它在Chrome中工作?
What I can do to make it works in chrome?
顺便说一句,当我尝试在 http://playground.html5rocks.com/#inline_workers b $ b和这次chrome工作,但firefox抱怨说:
BTW, when I tried out the sample at http://playground.html5rocks.com/#inline_workers and this time chrome works, but firefox complains that
错误:worker未定义源文件:
http://playground.html5rocks.com/ 第39行
推荐答案
我猜你试图在你的本地机器上运行,而不是在网络服务器上运行。工作人员受到同源政策的限制,但作为链接的维基百科页面笔记,
I'm guessing you're trying to run this on your local machine, not on a webserver. Workers are restricted by the Same Origin Policy, but as the linked Wikipedia page notes,
相同来源检查和相关机制的行为不是
,在很多情况下都是定义良好的,例如
没有明确定义的与
URL(文件:,data:等)相关联的主机名或端口。
The behavior of same-origin checks and related mechanisms is not well-defined in a number of corner cases, such as for protocols that do not have a clearly defined host name or port associated with their URLs (file:, data:, etc.).
加载本地文件(即使是相对URL)与使用文件:
协议加载文件相同。所以我的猜测是,问题是你试图加载 worker.js
作为本地文件 - Chrome不喜欢这个(出于一些安全的原因),尽管你可以通过像这样启动Chrome来强制这个问题: chrome.exe - 允许文件从文件访问
Loading a local file, even with a relative URL, is the same as loading a file with the file:
protocol. So my guess is that the problem is that you're trying to load worker.js
as a local file - Chrome doesn't like this (for some good security reasons), though you can force the issue by starting Chrome like this: chrome.exe --allow-file-access-from-files
或者,尝试在本地或远程网络服务器上处理脚本,看看是否解决了这个问题。 (如果你已经安装了Python,你可以进入相关目录并运行 python -m SimpleHTTPServer 8000
,然后进入 http:// localhost:8000 / )。
Alternatively, try serving your script on a local or remote webserver and see if that fixes the problem. (If you have Python installed, you can go to the directory in question and run python -m SimpleHTTPServer 8000
, then go to http://localhost:8000/ in your browser).
这篇关于HTML5网络工作者在Firefox 4中工作,但不在Chrome 12.0.742.122中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!