2

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?

2 Answers 2

4

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/

Sign up to request clarification or add additional context in comments.

3 Comments

Just to clarify, that jQueryify method is effectively doing the same thing as the some.library.js load call. Take a look at github.com/tmpvar/jsdom/blob/master/lib/jsdom.js#L34.
Just to save you guys some time: There are at the moment some issues with jsdom and nodejs >= 0.2.3. In the context of a script, window evaluates to {} because of the way sandboxing changed.
Thanks. It works. I should have carefully read the package's document.
0

I have a library that I'm working on which is a port of jquery to node - bit.ly/node-jquery

The goal is to do var $ = require('node-jquery') and use jquery libraries or functions as normal

1 Comment

Thanks, but I can use jQuery with jsdom.jQuerify(). I want to know how to evaluate a jQuery plugin's .js file in the node.js app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.