Accessing localStorage from a webWorker
Web workers only have access to the following:
XMLHttpRequest
- Application Cache
- Create other web workers
navigator
objectlocation
objectsetTimeout
methodclearTimeout
methodsetInterval
methodclearInterval
methodPerformance
object (mark
,measure
,now
methods:caniuse?
)IndexedDB
API (see:caniuse?
)importScripts
methodJSON
Worker
The window or parent objects are not accessible from a Web worker therefore you can't access the localStorage
.
To communicate between window and the workerglobalscope
you may use postMessage()
function and onmessage
event.
Accessing the DOM and window would not be thread safe, since the child thread would have the same privileges as its parent.
No, localStorage and sessionStorage are both undefined in a webworker process.
You would have to call postMessage()
back to the Worker's originating code, and have that code store the data in localStorage.
Interestingly, a webworker can use an AJAX call to send/retrieve info to/from a server, so that may open possibilities, depending on what you're trying to do.