1

JSDOM 版本:0.6.5 Nitrous.io Cloud IDE 环境

观察到的问题:创建一个空窗口,然后附加事件处理程序失败并出现异常跟踪:

TypeError: Cannot read property 'Node' of undefined

at Object.DOMWindow.addEventListener (/home/action/workspace/Projects/nodejs/node_modules/jsdom/lib/jsdom/browser/index.js:180:10)

at Request._callback (/home/action/workspace/Projects/nodejs/page_test_demo.js:22:14)

at Request.self.callback (/home/action/workspace/Projects/nodejs/node_modules/request/index.js:148:22)

at Request.EventEmitter.emit (events.js:98:17)

at Request. (/home/action/workspace/Projects/nodejs/node_modules/request/index.js:886:14)

at Request.EventEmitter.emit (events.js:117:20)

at IncomingMessage. (/home/action/workspace/Projects/nodejs/node_modules/request/index.js:837:12)

at IncomingMessage.EventEmitter.emit (events.js:117:20)

at _stream_readable.js:910:16

at process._tickCallback (node.js:415:13)

这是有问题的代码片段:

var window = jsdom.createWindow();
console.log(window);

window.addEventListener('load', function() {
console.log('Load event completed');
});

window.document = jsdom.jsdom(body);

谁能告诉我我在这里错过了什么?

4

1 回答 1

0

您在评论中提到的问题已在 7 个月前修复,因此只要您拥有 JSDOM 的最新版本,您应该能够做到这一点:

var jsdom = require('jsdom');
var body = '<html><body></body></html>';

var document = jsdom.jsdom(body);
var window = document.createWindow();
window.addEventListener('load', function() {
    console.log('Load event completed');
});
于 2013-07-13T16:54:16.227 回答