0

I am trying to implement web workers in my application but for some reason functions like alert and console error with undefined.

My web worker code:

AJAX = new XMLHttpRequest();

AJAX.open("GET", 'Record/fetchEntity', true);
AJAX.send();

var result = AJAX.responseText;
console.log(result); // Errors with 'console is not defined'.
self.postMessage(result);

By the way, result is an array of objects. Will this pass back ok to the master the way I have done it?

imperium2335
  • 23,402
  • 38
  • 111
  • 190

1 Answers1

2

Web workers have no direct access to the browser or DOM. There is a specific subset of browser related functions that they can access listed here in the MDN documentation

  • atob()
  • btoa()
  • clearInterval()
  • clearTimeout()
  • dump()
  • setInterval()
  • setTimeout()
  • XMLHttpRequest()
  • Worker()

Other than that they can only access the core standard JS functions and a few extra worker things

  • importScripts()
  • FileReaderSync()
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71