jQuery plugins with node.js

Problem

I’m using jQuery with jsdom in my node.js app.

Moreover, I want to use jQuery plugins(e.g. jQuery.diff), but I cannot find how to do this. Is there any way out?

Problem courtesy of: mhayashi

Solution

Create a script tag in your document to load your script into it. Example:

createWindow = function(fn) {
var window = jsdom.jsdom().createWindow(),
script = window.document.createElement('script');

jsdom.jQueryify(window, function() {
script.src = 'file://' + __dirname + '/some.library.js';
script.onload = function() {
if (this.readyState === 'complete') {
fn(window);
}
}
});
}

createWindow(function(window) {
// Do your jQuery stuff:
window.$('body').hide();
});

Source: http://blog.davidpadbury.com/2010/10/03/using-nodejs-to-render-js-charts-on-server/

Solution courtesy of: balu

View additional discussion.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.