# jsdom old API The old jsdom API, from before v10, is hard to use and understand, and has bad defaults. But for now, it still has more features than the new API introduced in v10. As such, it is still supported, until we can port over all important features (notably custom resource loading) to the new API. No new features will be added to the old API, but bug reports are accepted. ## 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/lib/old-api.js"); 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/lib/old-api.js"); 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/lib/old-api.js"); 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/lib/old-api.js"); 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 `