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

Active3 hr before
Viewed126 times

8 Answers


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.,Javascript, generally, can't access anything outside of the url that the javascript file came from.,According to the Web Worker draft specification, workers must be hosted at the same domain as the "first script", that is, the script that is creating the worker. The URL of the first script is what the worker URL is resolved against., " can try to work around loading javascript files from other urls and then see if the web worker works from an absolute url." Can you elaborate on that? I don't understand what sort of workarounds you're talking about. – mattblodgett Oct 11 '09 at 2:31

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

var myWorker = new Worker("");
load more v

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:,I haven't been able to get something like this to work:,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.,Failed to load script: (nsresult = 0x805303f4)

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

var myWorker = new Worker("");
load more v

Can I load a web worker script from an absolute URL? - The answer over here is no.,However there is a hack I found to do it anyway:,Is there any better way to do that?,Get answers to millions of questions and give back by sharing your knowledge with others.

However there is a hack I found to do it anyway:

var worker; //= new Worker('http://xyz/FSWorker.js');
var xhr = new XMLHttpRequest();"GET", 'http://xyz/FSWorker.js');
xhr.responseType = 'blob';
xhr.onload = function(e) {
   var blob = this.response;
   worker = new Worker(window.URL.createObjectURL(blob));
   fs_ready(); // do more stuff here


Taking this one step further, we can get clever with how the worker's JS code is inlined in our page. This technique uses a <script> tag to define the worker:,You can load external script files or libraries into a worker with the importScripts() function. The method takes zero or more strings representing the filenames for the resources to import.,When using these techniques to inline your worker code, importScripts() will only work if you supply an absolute URI. If you attempt to pass a relative URI, the browser will complain with a security error. The reason being: the worker (now created from a blob URL) will be resolved with a blob: prefix, while your app will be running from a different (presumably http://) scheme. Hence, the failure will be due to cross origin restrictions.,Importing external scripts using the importScripts() method

Web Workers run in an isolated thread. As a result, the code that they execute needs to be contained in a separate file. But before we do that, the first thing to do is create a new Worker object in your main page. The constructor takes the name of the worker script:

var worker = new Worker('task.js');
load more v

Defines the absolute location of the script executed by the Worker.,Represents a specific kind of worker that can be accessed from several browsing contexts, being several windows, iframes or even workers.,Represents the scope of a shared worker, inheriting from WorkerGlobalScope and adding some dedicated features.,There are a number of different types of workers:

load more v

There are a variety of uses that workers can be put to. The following subsections show various examples of this use.,A WorkerLocation object represents an absolute URL set at its creation.,The location attribute must return the WorkerLocation object created for the WorkerGlobalScope object when the worker was created. It represents the absolute URL of the script that was used to initialize the worker, after any redirects.,For each url in the resulting absolute URLs, run these substeps:

The postMessage() method's first argument can be structured data:

   opcode: 'activate',
   device: 1938,
   parameters: [23, 102]

Unlike child_process or cluster, worker_threads can share memory. They do so by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.,This is the final event emitted by any Worker instance.,Creating Worker instances inside of other Workers is possible.,The Worker class represents an independent JavaScript execution thread. Most Node.js APIs are available inside of it.

Within a worker thread, worker.getEnvironmentData() returns a clone of data passed to the spawning thread's worker.setEnvironmentData(). Every new Worker receives its own copy of the environment data automatically.

const {
} = require('worker_threads');

if (isMainThread) {
   setEnvironmentData('Hello', 'World!');
   const worker = new Worker(__filename);
} else {
   console.log(getEnvironmentData('Hello')); // Prints 'World!'.
load more v

You will likely need to set the baseUrl configuration option to make sure require() can find the scripts to load.,RequireJS loads each dependency as a script tag, using head.appendChild().,The data-main attribute is a special attribute that require.js will check to start script loading:,In the future, this code may be pulled into the require/ directory as an optional module that you can load in your env to get the right load behavior based on the host environment.

If the module does not have any dependencies, and it is just a collection of name/value pairs, then just pass an object literal to define():

//Inside file my/shirt.js:
   color: "black",
   size: "unisize"
load more v

Other "undefined-undefined" queries related to "Can I load a web worker script from an absolute URL?"