admin管理员组

文章数量:1355540

I'm trying to follow the very short, simple setup instructions at under section "Creating a browser-like BOM/DOM/Window". Unfortunately, after the 3rd line (the .createWindow step), I do console.log(window) and it prints an empty object. There should be at least window.document there, but there isn't. It seems as though document.createWindow is acting the same as jsdom.createWindow.

Here's my code so far:

var jsdom = require('jsdom').jsdom;

document = jsdom("<html><head></head><body>hello world</body></html>");
window = document.createWindow();

console.log(window); // output: {}

console.log(window.document.innerHTML); // error, cannot read innerHTML on undefined

So, what stupid thing am I doing wrong?

FYI document is created correctly. Printing it outputs a very large object.

I am using a Mac.

I'm trying to follow the very short, simple setup instructions at https://github./tmpvar/jsdom under section "Creating a browser-like BOM/DOM/Window". Unfortunately, after the 3rd line (the .createWindow step), I do console.log(window) and it prints an empty object. There should be at least window.document there, but there isn't. It seems as though document.createWindow is acting the same as jsdom.createWindow.

Here's my code so far:

var jsdom = require('jsdom').jsdom;

document = jsdom("<html><head></head><body>hello world</body></html>");
window = document.createWindow();

console.log(window); // output: {}

console.log(window.document.innerHTML); // error, cannot read innerHTML on undefined

So, what stupid thing am I doing wrong?

FYI document is created correctly. Printing it outputs a very large object.

I am using a Mac.

Share Improve this question edited Jul 2, 2012 at 21:25 Anonymous asked Jul 2, 2012 at 21:02 AnonymousAnonymous 3,4143 gold badges38 silver badges52 bronze badges 2
  • this works for me and not sure why are you getting those errors. – Kishore Commented Jul 2, 2012 at 21:17
  • Node.js version and jsdom version? – Ryan Olds Commented Jul 3, 2012 at 16:08
Add a ment  | 

2 Answers 2

Reset to default 5
var jsdom = require('jsdom').jsdom;
document = jsdom('<!doctype html><html><body>hello jsdom</body></html>');
window = document.createWindow();
console.log(window);
var txt = window.document.body.innerHTML;
console.log(txt);

Problem was due to an installation error with Contextify. I'm used to developing on either Windows or *NIX and on Mac, due to *NIX terminal, assumed I would have things like Make. Problem was solved by installing Xcode with mand line tools, which installs Make among other things, which solved installation error for Contexify, which resolved the overall issue.

本文标签: javascriptjsdom documentcreateWindow() returns empty for nonempty documentStack Overflow