Cross Domain Web Worker? [duplicate]

Active3 hr before
Viewed126 times

7 Answers


I also have the same problem, hope this can help you,Do you have allow methods set?,Since starting writing this question, I have got around this problem by ajaxing the script, blobbing it and running it from that, but am I missing something in the original idea?,It's pretty funny, but importScripts() inside the worker does not have the same origin restriction. So I used this plain workaround:

I also have the same problem, hope this can help you

 function XHRWorker(url, ready, scope) {
    var oReq = new XMLHttpRequest();
    oReq.addEventListener('load', function() {
       var worker = new Worker(window.URL.createObjectURL(new Blob([this.responseText])));
       if (ready) {
, worker);
    }, oReq);"get", url, true);

 function WorkerStart() {
    XHRWorker("", function(worker) {
       worker.postMessage("hello world");
       worker.onmessage = function(e) {
    }, this);

load more v

The worker thread is killed immediately.,Creating a new worker is simple. All you need to do is call the Worker() constructor, specifying the URI of a script to execute in the worker thread (main.js):,Debugging worker threads,If you need to immediately terminate a running worker from the main thread, you can do so by calling the worker's terminate method:

if (window.Worker) {


load more v

Loading using importScripts seems to work fine on Edge and Chrome. importScripts from another domain seems to fail - sometimes - for some reason - in Firefox. Still trying to work out why ...

load more v

Browsers throw security exceptions.,Domain1 runs some javascript code that invokes:,Domain2 serves a page containing javascript with this header:,Somewhat simpler than fetching script manually.

Domain2 serves a page containing javascript with this header:

"Access-Control-Allow-Origin: *"

Domain1 runs some javascript code that invokes:

new Worker("//")
load more v

The W3C and WHATWG envision web workers as long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions. Keeping such workers from being interrupted by user activities should allow Web pages to remain responsive at the same time as they are running long tasks in the background. ,The simplest use of web workers is for performing a computationally expensive task without interrupting the user interface. ,The web worker specification is part of the HTML Living Standard.[1] ,As envisioned by WHATWG, web workers are relatively heavy-weight and are not intended to be used in large numbers. They are expected to be long-lived, with a high start-up performance cost, and a high per-instance memory cost.[1]

var worker = new Worker("worker_script.js");
load more v

A SharedWorker is a special Web Worker, in that multiple tabs or windows of the same origin can reference the same SharedWorker. The API is pretty much impossible to polyfill and has only ever been implemented in Blink, so I won’t be paying any attention to it in this article.,Before we get more into that, it’s important to note that Web Workers, Service Workers and Worklets are similar, but ultimately different things for different purposes:,In this article, I am exclusively talking about WebWorkers (often just “Worker” for short). A worker is an isolated JavaScript scope running in a separate thread. It is spawned (and owned) by a page.,There are some questions and thoughts that are raised quite often, so I wanted to preempt them and record my answer here.

Note: RAIL has been a guiding framework for 6 years now. It’s important to note that 60fps is really a placeholder value for whatever the native refresh rate of the user’s display is. For example, some of the newer pixel phones have a 90Hz screen and the iPad Pro has a 120Hz screen, reducing the frame budget to 11.1ms and 8.3ms respectively.

To complicate things further, there is no good way to determine the refresh rate of the device that your app is running on apart from measuring the amount of time that elapses between requestAnimationFrame() callbacks.*

load more v

Web Workers specification,Introducing Web Workers: Bring Threading to JavaScript,For an sample of how to spawn a subworker, see the example in the specification.,Background I/O or polling of webservices

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

Other "duplicate-cross" queries related to "Cross Domain Web Worker? [duplicate]"