开发者

nodejs + jsdom, jQuery strange behavior

开发者 https://www.devze.com 2023-03-01 10:23 出处:网络
The code below is just a small snippet from my server.js file just to run the test provided by the jsdom documentation.

The code below is just a small snippet from my server.js file just to run the test provided by the jsdom documentation.

var window = jsdom.jsdom().createWindow();
jsdom.jQueryify(window, './jq.min.js' , function() {
  console.log('inside');
  window.$('body').append('<div class="testing">Hello World, It works</div>');
  console.log(window.$('.testing').text());
  console.log('end');
});

The output I get literally is just inside and then the server hangs and never returns. I've added a debug statement console.log(window); to see if the window object is truly being created, and I do end up with a fairly large output statement detailing the object's contents. One thing I did notice however is that the output does not show that $ is a defined method of the window object and in fact, console.log(window.$); renders undefined.

I understand jsdom is still in dev mode, but is there something I'm missing here?

Just as some background, I have tried several variations of the code, includin开发者_JAVA技巧g using the jsdom.env() method and also building a document from existing HTML markup, neither of which rendered expected results either.


I hope this code snippet helps you:

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();
});

from here

0

精彩评论

暂无评论...
验证码 换一张
取 消