I have some javascript code like this,
var worker = new Worker("javascript/worker.js");
worker.onmessage = function(evt)
{
// stuff
}
worker.js looks like this,
importScripts("base.js");
function getImage()
{
$.ajax({
url: 'URL'
dataType: "text/plain; charset=x-user-defined",
mimeType: "text/plain; charset=x-user-defined",
success: function(data, textStatus, jqXHR)
{
callback();
}
});
}
The worker.js file does not have jQuery included so that doesn't work. If I add this to worker.js,
importScripts("jQuery.js");
Then I get the message,
Uncaught ReferenceError: window is not defined
I'm not really familiar with workers. Am I right in thinking this it is loading the worker.js code in a completely separate environment (basically a background thread) so it doesn't have access to window.
I have some javascript code like this,
var worker = new Worker("javascript/worker.js");
worker.onmessage = function(evt)
{
// stuff
}
worker.js looks like this,
importScripts("base.js");
function getImage()
{
$.ajax({
url: 'URL'
dataType: "text/plain; charset=x-user-defined",
mimeType: "text/plain; charset=x-user-defined",
success: function(data, textStatus, jqXHR)
{
callback();
}
});
}
The worker.js file does not have jQuery included so that doesn't work. If I add this to worker.js,
importScripts("jQuery.js");
Then I get the message,
Uncaught ReferenceError: window is not defined
I'm not really familiar with workers. Am I right in thinking this it is loading the worker.js code in a completely separate environment (basically a background thread) so it doesn't have access to window.
Share Improve this question edited Feb 9, 2012 at 2:41 Joe 15.8k4 gold badges50 silver badges83 bronze badges asked Feb 9, 2012 at 2:39 peterpeter 13.5k24 gold badges85 silver badges143 bronze badges 2- Not an answer to your question, but do you really need a worker in this example? AJAX requests already execute asynchronously. – millimoose Commented Feb 9, 2012 at 2:48
- Perhaps you are right. I was taking some existing code and modifying it. The existing code was using an XMLHttpRequest which is not asynchronous I am guessing? The answer might be glaring me in the face after all. – peter Commented Feb 9, 2012 at 2:51
3 Answers
Reset to default 14On the worker's .js file:
importScripts('../relative/path/lib.min.js', '../../other/lib.js');
In order to prevent web workers from running into concurrency problems, the web worker spec prevents the worker from having access to the window object or the DOM.
The only objects and methods available inside a worker are:
- The navigator object
- The location object
- XMLHttpRequest
- The setTimeout and clearTimeout functions.
- The Application Cache
- Spawning other Web Workers
- Using a webworker specific method to load other scripts
So whilst you could use the worker to create the XMLHttpRequest manually; Jquery or any other library which expects to be able to access the DOM or Window Object is never going to work in there.
Yeah it has been correctly pointed out to me that the ajax call is asynchronous so the worker is not required. For circumstances which I won't explain turns out that the ajax call didn't work anyway, so I reverted back to the XMLHttpRequest how it was and left it using a worker.