tags:

views:

4754

answers:

6

Is it possible to use jQuery selectors/dom manipulation server-side using node.js?

A: 

Not that I know of. The DOM is a client side thing (jQuery doesn't parse the HTML, but the DOM).

Here are some current Node.js projects:

http://wiki.github.com/ry/node

And SimonW's djangode is pretty damn cool...

Nosredna
I wish it was possible. I already tried including jquery on a node.js project and of course it didn't work. jQuery is based on document/window.Rhino is capable of running jQuery server side:http://ejohn.org/blog/bringing-the-browser-to-the-server/ I'm going to look for more parsers. Maybe there is one that doesn't depend on the browser.
John
Have you tried asking on the google group? http://groups.google.com/group/envjs
Nosredna
@John: the only reason jQuery can run on Rhino is because of this project: http://github.com/jeresig/env-js/blob/master/src/env.js It simulates a small portion of the DOM and the JavaScript runtime. It relies on Java apis so is a no-go for Node.js (which uses V8/C++).
Crescent Fresh
+4  A: 

An alternative is to use Underscore.js. It should provide what you might of wanted server-side from JQuery.

John Wright
+3  A: 

No. It's going to be quite a big effort to port a browser environment to node.

Another approach, that I'm currently investigating for unit testing, is to create "Mock" version of jQuery that provides callbacks whenever a selector is called.

This way you could unit test your jQuery plugins without actually having a DOM. You'll still have to test in real browsers to see if your code works in the wild, but if you discover browser specific issues, you can easily "mock" those in your unit tests as well.

I'll push something to github.com/felixge once it's ready to show.

felixge
I like this idea... it's should be quite easy to do.
Sudhir Jonathan
+2  A: 

Since it is now possible to run YUI on Node.js, perhaps jQuery can be ported to.

Mikael Karon
+3  A: 

I believe the answer to this is now yes.
http://github.com/tmpvar/jsdom/blob/master/example/jquery/run.js

var navigator = { userAgent: "node-js" };  
var jQuery = require("./node-jquery").jQueryInit(window, navigator);
rdzah
I'm sorry to report that it is going to take more work to get jQuery running on jsdom. Sizzle however does work! I really want to keep jsdom as light as possible, so adding in full browser emulation like env.js is not really a priority at this time.
tmpvar
+9  A: 

Using this library http://github.com/tmpvar/jsdom you now can. Just look at their jquery example in the examples directory.