# jsdom A JavaScript implementation of the WHATWG DOM and HTML standards, for use with [Node.js](https://nodejs.org/). ## Install ```bash $ npm install jsdom ``` Note that as of our 7.0.0 release, jsdom requires Node.js 4 or newer ([why?](https://github.com/tmpvar/jsdom/blob/master/Changelog.md#700)). In the meantime you are still welcome to install a release in [the 3.x series](https://github.com/tmpvar/jsdom/tree/3.x) if you use legacy Node.js versions like 0.10 or 0.12. There are also various releases between 3.x and 7.0.0 that work with various io.js versions. ## Human contact - [Mailing list](http://groups.google.com/group/jsdom) - IRC channel: [#jsdom on freenode](irc://irc.freenode.net/jsdom) ## Easymode: `jsdom.env` `jsdom.env` is an API that allows you to throw a bunch of stuff at it, and it will generally do the right thing. You can use it with a URL ```js // Count all of the links from the io.js build page var jsdom = require("jsdom"); jsdom.env( "https://iojs.org/dist/", ["http://code.jquery.com/jquery.js"], function (err, window) { console.log("there have been", window.$("a").length - 4, "io.js releases!"); } ); ``` or with raw HTML ```js // Run some jQuery on a html fragment var jsdom = require("jsdom"); jsdom.env( '

jsdom!

', ["http://code.jquery.com/jquery.js"], function (err, window) { console.log("contents of a.the-link:", window.$("a.the-link").text()); } ); ``` or with a configuration object ```js // Print all of the news items on Hacker News var jsdom = require("jsdom"); jsdom.env({ url: "http://news.ycombinator.com/", scripts: ["http://code.jquery.com/jquery.js"], done: function (err, window) { var $ = window.$; console.log("HN Links"); $("td.title:not(:last) a").each(function() { console.log(" -", $(this).text()); }); } }); ``` or with raw JavaScript source ```js // Print all of the news items on Hacker News var jsdom = require("jsdom"); var fs = require("fs"); var jquery = fs.readFileSync("./path/to/jquery.js", "utf-8"); jsdom.env({ url: "http://news.ycombinator.com/", src: [jquery], done: function (err, window) { var $ = window.$; console.log("HN Links"); $("td.title:not(:last) a").each(function () { console.log(" -", $(this).text()); }); } }); ``` ### How it works The do-what-I-mean API is used like so: ```js jsdom.env(string, [scripts], [config], callback); ``` - `string`: may be a URL, file name, or HTML fragment - `scripts`: a string or array of strings, containing file names or URLs that will be inserted as ` ``` For more details, see the discussion in [#640](https://github.com/tmpvar/jsdom/issues/640), especially [@matthewkastor](https://github.com/matthewkastor)'s [insightful comment](https://github.com/tmpvar/jsdom/issues/640#issuecomment-22216965). #### Listening for script errors during initialization Although it is easy to listen for script errors after initialization, via code like ```js var window = jsdom.jsdom(...).defaultView; window.addEventListener("error", function (event) { console.error("script error!!", event.error); }); ``` it is often also desirable to listen for any script errors during initialization, or errors loading scripts passed to `jsdom.env`. To do this, use the virtual console feature, described in more detail later: ```js var virtualConsole = jsdom.createVirtualConsole(); virtualConsole.on("jsdomError", function (error) { console.error(error.stack, error.detail); }); var window = jsdom.jsdom(..., { virtualConsole }).defaultView; ``` You also get this functionality for free by default if you use `virtualConsole.sendTo`; again, see more below: ```js var virtualConsole = jsdom.createVirtualConsole().sendTo(console); var window = jsdom.jsdom(..., { virtualConsole }).defaultView; ``` ### On running scripts and being safe By default, `jsdom.env` will not process and run external JavaScript, since our sandbox is not foolproof. That is, code running inside the DOM's `