Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

javascript - Can I load a web worker script from an absolute URL?

I haven't been able to get something like this to work:

var myWorker = new Worker("http://example.com/js/worker.js");

In my Firebug console, I get an error like this:

Failed to load script: http://example.com/js/worker.js (nsresult = 0x805303f4)

Every example of web worker usage I've seen loads a script from a relative path. I tried something like this, and it works just fine:

var myWorker = new Worker("worker.js");

But what if I need to load a worker script that's not at a relative location? I've googled extensively, and I haven't seen this issue addressed anywhere.

I should add that I'm attempting to do this in Firefox 3.5.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For those that don't know, here is the spec for Web Worker: http://www.whatwg.org/specs/web-workers/current-work/

And a post by John Resig: http://ejohn.org/blog/web-workers/

Javascript, generally, can't access anything outside of the url that the javascript file came from.

I believe that is what this part of the spec means, from: http://www.w3.org/TR/workers/

4.2 Base URLs and origins of workers

Both the origin and effective script origin of scripts running in workers are the origin of the absolute URL given in that the worker's location attribute represents.

This post has a statement about what error should be thrown in your situation: http://canvex.lazyilluminati.com/misc/cgi/issues.cgi/message/%3Cop.u0ppu4lpidj3kv@zcorpandell.linkoping.osa%3E


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...