HTML Web Worker and Jquery Ajax call HTML Web Worker and Jquery Ajax call ajax ajax

HTML Web Worker and Jquery Ajax call


No you cannot. There's no access to non-thread safe components or the DOM and you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code. jQuery is a JavaScript DOM library.

But you can use a native XMLHttpRequest in your worker however.

And, importing external scripts does not go via the page with a script tag : use importScripts() for that in your worker file.


Here's what I found:

You can load external script files or libraries into a worker with the importScripts() function.

http://www.html5rocks.com/en/tutorials/workers/basics/#toc-enviornment-loadingscripts

importScripts('script1.js');importScripts('script2.js');

or

importScripts('script1.js', 'script2.js');

Although, you cannot load jQuery, because jQuery requires DOM access, which web workers don't have.


Since web workers are in external files, they do not have access to the following JavaScript objects:

  • The window object
  • The document object
  • The parent object

So you can't use $ inside worker file. Better you can use traditional AJAX something like this

if (window.XMLHttpRequest){  // code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();}else{  // code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}

Reference at http://www.w3schools.com/html/html5_webworkers.asp