Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the document object to a web worker

I know that web workers cant access the dom directly. But would it be a bad idea to do something like this:

var doc = $(document);

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

worker.postMessage({ cmd: 'doDomStuff', data: doc });

Do you see any downsides with this peice of code?

Any tips/comments are much appreciated.

update: Just to be clear: I only want to getdata from the DOM, not set any new values or manipulate the DOM in any way.

like image 466
Johan Avatar asked Aug 08 '12 21:08

Johan


Video Answer


1 Answers

I don't see any reason why you can't do this, but this could lead to issues when you are trying to manipulate the same element in the worker and in the main js code at the same time.

You would need to add some mutex locking to your code.

Sorry scratch the above...


Workers do NOT have access to:

The DOM (it's not thread-safe)
The window object
The document object
The parent object

Source

like image 73
Naftali Avatar answered Sep 30 '22 03:09

Naftali