Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use jQuery to Parse XML from within a Web Worker Thread
    text
    copied!<p><em><strong>Skimming? Just read what's bold.</em></strong></p> <p>Howdy friends! I'm building a <strong>web application</strong> that needs to do some serious XML crunching.</p> <p>I currently have a function that <strong>uses jQuery to parse and manipulate massive XML documents.</strong> This <strong>locks up the brower's UI for up to sixty seconds</strong>, which is obviously, quite lame.</p> <p>I've been looking into <a href="https://developer.mozilla.org/en-US/docs/DOM/Using_web_workers" rel="nofollow"><em>Web Workers</em></a>, because <strong>I'd like to run this operation in a Web Worker thread to alleviate the browser's UI.</strong></p> <p>I was <strong>hoping I could use the Web Worker's <code>self.importScripts()</code> to import the jQuery library, to parse and manipulate XML from within the Web Worker thread.</strong> <br />&nbsp;&nbsp; <strong>Cannot do:</strong> the <strong>Web Worker throws errors, like <code>window is not defined</code>,</strong> because the jQuery library makes references to window and friends, which aren't thread safe, and therefore aren't present in the Web Worker's global namespace.</p> <p>I've done a lot of Googling, but have had <strong>trouble finding any recent information on this subject.</strong> I've found some hacky work-arounds and jQuery plugins, but they are all several years old, and I've been hoping I've been missing a newer solution for this relatively new technology. <br />&nbsp;&nbsp; <strong>Does a (semi-)official <em>"Web Worker jQuery"</em> exist?</strong></p> <p>I've found <a href="http://xmljs.sourceforge.net/index.html" rel="nofollow"><em>XMLforScript</em></a>, an alternative to jQuery that should run within a Web Worker -- but <strong>I'm specifically looking for a jQuery solution,</strong> because I don't want to rewrite my code.</p> <p>I was tempted to try to create a series of fake elements to ease jQuery's errors within the Web Worker, before calling <code>importScripts()</code>, like so:</p> <pre><code>window={ document:{ createElement:function(){ return { setAttribute:function(){} } } } }; /* ... much more fake DOM */ importScripts('jquery.js'); </code></pre> <p>But, I realize I'd have to go really deep into creating a gigantic artificial DOM to satisfy jQuery, and figure it's probably a road to failure and time-wasting, because jQuery probably relies on some of these objects for it's XML parsing (I don't know enough about how jQuery works to know).</p> <p><strong>Does anybody have any lovely solutions?</strong></p> <p>If not, I'll just make a fatty static loading indicator to warn the user that shenanigans take time, and that they have to trust and hope the web-app hasn't crashed or stopped working :D</p> <p>Thanks for your ponderings. <br />&nbsp;&nbsp;//Chase.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload