5

I'd like to be able to use a number of utility methods from jQuery in a Web Worker, where there is no access to the window or document objects.

Specifically, I'd like to be able to use methods like $.extend(), $.ajax(), and the entire $.Deferred() system. I obviously would not need any of the DOM traversal and manipulation methods, so I'm not looking for a solution like JSDOM.

I can extract the non-DOM portions of jQuery myself, but this is a pain to maintain. Are there any available distributions or build scripts for jQuery that just build the non-DOM portions?

kpozin
  • 25,691
  • 19
  • 57
  • 76
  • Couldn't you just use the standard version of jQuery, and just avoid calling any operations that rely upon `window` and `document`? – aroth Jul 30 '11 at 05:50
  • @aroth, that's difficult because it looks like a lot of jQuery's initialization code (not just methods that are explicitly called by client code) relies on the presence of the full browser API. – kpozin Jul 30 '11 at 05:53
  • It looks like like [jQuery.Hive](https://github.com/rwldrn/jquery-hive/blob/master/jquery.hive.pollen.js) tries to do this in part, but they're rewriting a lot of jQuery methods and don't seem to be synced to the jQuery release cycle. – kpozin Jul 30 '11 at 06:00
  • I see. Perhaps you could stub-out the features that the initialization code relies upon, like `window = {};` and `document = {};`? I haven't looked at exactly what things the init code requires, so I don't know if this would be a manageable task or not. – aroth Jul 30 '11 at 06:02
  • Why would you want to do this? There are better alternative libraries. Don't use jQuery on the server environment. – Raynos Jul 31 '11 at 11:33
  • @Raynos, I mentioned that I'd like to use this primarily in Web Workers (though if others find a use for it on the server, I'm not opposed). When I'm writing client-side code, I'd like to stick to a single framework regardless of whether the code is running in a `DOMWindow` or a `WorkerContext`. – kpozin Jul 31 '11 at 14:57
  • @kpozin "a single framework" jQuery is not a framework :(. I see your webworker use-case though. It may be worthwhile to raise this as a bug on the jQuery website. – Raynos Jul 31 '11 at 15:06

2 Answers2

3

My answer: https://github.com/kpozin/jquery-nodom.

So far, I have it working and tested with $.Deferred() and a subset of $.ajax() inside of a Web Worker context. Documentation and possible test suites coming later.

An initial test build is available: jquery.nodom.js

kpozin
  • 25,691
  • 19
  • 57
  • 76
  • Cool! I see the last commit was a few years ago - is that because there haven't been any relevant changes in newer versions of jQuery? Or is this project not being maintained? – Jason Kleban Jul 07 '13 at 14:34
  • 1
    I haven't been maintaining this because I haven't needed it in a while. For `$.Deferred`, a good non-DOM alternative is [underscore.Deferred](https://github.com/wookiehangover/underscore.Deferred). If anyone comes across an `XMLHttpRequest` wrapper that matches the `$.ajax` API, please comment. – kpozin Jul 07 '13 at 23:05
0

so jQuery is open source, you could easily clone the git repo, create a build repo that plucks the parts you like (I believe jQuery development is broken up into modules) and just revise the build system as you keep your jQuery clone up to date. You could then open source that sucker and solve this problem for good. It looks like someone has already maybe addressed this particular question in this thread.

Community
  • 1
  • 1
Gabriel
  • 18,322
  • 2
  • 37
  • 44
  • Here's the [solution](http://stackoverflow.com/questions/6881344/non-dom-subset-of-jquery/6887621#6887621). – kpozin Jul 31 '11 at 05:01