51

I am using this Web worker which has a Global variable declared in it. Can I access the same (Global variable in worker 1) in the newly spawned web worker(worker 2)?

When I've tried using jQuery in web worker, I get error "window is not defined". Is there any way to use jQuery in a Web Worker?

importScripts('jquery-latest.js'); function fetch_ajax(url) { $.ajax({ type: 'GET', url: url, success: function(response) { postMessage(response); } }); } fetch_ajax('test.txt'); 
1
  • You seem to have asked two questions here. StackOverflow questions should be kept separate. I'm pretty sure the jQuery question is already answered, so it's probably best just to remove that part from your question. Commented Jan 12, 2015 at 22:01

4 Answers 4

85

Web Workers don't have a window object.

To access global state, use self instead, code that will work on both the main thread and the worker thread.

But note that you still won't be able to access or manipulate the parent DOM (e.g. get window.jQuery via self.jQuery).

While the main thread window self points to the Window object, in worker threads self points to a separate WorkerGlobalScope object.

Sign up to request clarification or add additional context in comments.

4 Comments

Tnx I practically tested and found I am unable to access a variable in worker1 in worker2. And also unable to use jQuery as there is no access to window object. Instead, I used native XHR and returned the response successfully...
And it's a good thing. Communication with workers is via messages (PostMessage method and onmessage event). Access to DOM or some shared variables would result in lots of nasty errors...
My understanding is that the global scope in each web worker, and in the main thread (the web page), are all separate, but that in each case, they can be accessed via the self object. In the main thread, self is a synonym for window. Is this your understanding as well, or did you mean something else?
@MichaelScheper The self property of a global object basically returns this (perhaps not literally, but conceptually). So, window.selfreturns window, and self inside a Web Worker returns the global object of the worker (whatever that's called).
23

Based on @buley tip, I did it:

var window = self; importScripts(/* dependencies here */); /* my code */ 

In my case I was trying to use the ES6-Promise lib: https://github.com/jakearchibald/es6-promise#readme

2 Comments

This "tip" is somewhat risky.For some different purpose (sharing code), I am using the test typeof window !== 'undefined' to check if I am in a web worker, or in the main client-side JavaScript. It wouldn't work anymore.
@allezl'OM It might be better to check for window.document.
0

Instead of global variables in web workers, you should use postmessage & onmessage api for information exchange between web workers.
Regarding your jQuery issue, jQuery is for Dom manipulation and Dom isn't available to web workers. I think you need to change your approach. However, if you insist on using jQuery in the web worker you can try something like what is explained at: https://anandabandaru.wordpress.com/2019/06/21/use-jquery-inside-html5-web-worker/

Comments

0

Since web workers deal with threading, they want to simplify things by blocking access to shared variables i think so they don't have to deal with synchronizing different processes when they access the same variables.

The closest you can get to "global" is BroadcastChannel.

this runs on global channel identifiers, then you can listen to pub/sub on these.

const channel = new BroadcastChannel('my_channel'); channel.postMessage({ question: "what is the value of the variable" }); channel.onmessage = function (e) { console.log('Received read ', e.data); }; 

The cool thing is you can subscribe or send messages on "my_channel" anywhere.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.