Jsdom: window.document.innerHTML -> is undefined

Created on 6 Sep 2014  路  1Comment  路  Source: jsdom/jsdom

I installed jsdom via npm and required it in my testfile.

Everything is fine so far. Then I try to run an example from the documentation:

var jsdom = require("jsdom").jsdom;
var document = jsdom("hello world");
var window = document.parentWindow;

console.log(window.document.innerHTML);
// output: "<html><head></head><body>hello world</body></html>"

console.log(window.innerWidth);
// output: 1024

console.log(typeof window.document.getElementsByClassName);
// outputs: function

I get the two last outputs right, but the first one doesn`t work for me.

bildschirmfoto 2014-09-06 um 12 16 26

I also tried to attach the document and the window from jsdom to the node global object, but nothing changes here:

var jsdom = require("jsdom").jsdom;
global.document = jsdom("hello world");
global.window = document.parentWindow;

console.log(window.document.innerHTML);
// output: "<html><head></head><body>hello world</body></html>"

console.log(window.innerWidth);
// output: 1024

console.log(typeof window.document.getElementsByClassName);
// outputs: function

I can`t even find something like innerHTML when I look into the whole jsdom-object.

Would be awesome if anybody can tell me the problem.

Thanks a lot
Max

Most helpful comment

Hey me again ^^,

found the problem. Would be helpful if the example in the documentation actually would be a working example.

Something like this maybe:

var jsdom = require("jsdom").jsdom;
var document = jsdom("<div class='selector'><div class='innerselector'></div>");
var window = document.parentWindow;

console.log(window.document.querySelector('body').innerHTML);
// output: "<div class="selector"><div class="innerselector"></div></div>"

console.log(window.innerWidth);
// output: 1024

console.log(typeof window.document.getElementsByClassName);
// outputs: function

cheers Max

>All comments

Hey me again ^^,

found the problem. Would be helpful if the example in the documentation actually would be a working example.

Something like this maybe:

var jsdom = require("jsdom").jsdom;
var document = jsdom("<div class='selector'><div class='innerselector'></div>");
var window = document.parentWindow;

console.log(window.document.querySelector('body').innerHTML);
// output: "<div class="selector"><div class="innerselector"></div></div>"

console.log(window.innerWidth);
// output: 1024

console.log(typeof window.document.getElementsByClassName);
// outputs: function

cheers Max

Was this page helpful?
0 / 5 - 0 ratings