Html 此WebWorker有什么问题(没有错误,但未访问console.log)
我有以下代码,试图测试WebWorkers。我有一个index.html文件,如下所示:Html 此WebWorker有什么问题(没有错误,但未访问console.log),html,web-worker,Html,Web Worker,我有以下代码,试图测试WebWorkers。我有一个index.html文件,如下所示: <html> <head></head> <body> <script type='text/javascript'> var worker = new Worker('./myworker.js'); console.log('after creation'); worker.addEve
<html>
<head></head>
<body>
<script type='text/javascript'>
var worker = new Worker('./myworker.js');
console.log('after creation');
worker.addEventListener('message', function(msg){
console.log(msg);
});
worker.postMessage();
</script>
</body>
</html>
当我加载index.html(在Chrome14中)时,“创建后”console.log永远不会发生。也没有别的。Console.log发生在创建新Worker()之前,但之后似乎什么也没有发生 好吧,给我的饼干涂黄油,显然,当本地加载(例如从文件://)时,WebWorkers不工作
来源:(内容底部)如果你在chrome中测试应用程序,你应该使用
启动chrome——允许从文件访问文件,或者使用本地服务器测试应用程序,因为Google chrome的安全限制,工作人员不会在本地运行(例如,从文件://)在最新版本的浏览器中。相反,它们会以静默方式失败!若要从文件://scheme运行应用程序,请使用--allow file access from files标志集运行Chrome。注意:不建议使用此标志集运行主浏览器。它应仅用于测试目的,而不用于常规浏览。其他浏览器不会强制使用此标志集他有同样的限制
this.onmessage = function(){
postMessage('got the msg, thanks');
};