Skip to content

Instantly share code, notes, and snippets.

@aih
Forked from clarkdave/parseAndModifyHtml.js
Created May 8, 2011 17:27
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aih/961523 to your computer and use it in GitHub Desktop.
Save aih/961523 to your computer and use it in GitHub Desktop.
Using node.js to parse HTML with jsdom and modify it with jQuery
/**
* npm install jsdom
* npm install jquery
*/
var html = "<!doctype html><html><body><h1>Hello world!</h1></body></html>";
/* parse the html and create a dom window */
var window = require('jsdom').jsdom(html, null, {
// standard options: disable loading other assets
// or executing script tags
FetchExternalResources: false,
ProcessExternalResources: false,
MutationEvents: false,
QuerySelector: false
}).createWindow();
/* apply jquery to the window */
var $ = require('jquery').create(window);
/* modify html using jquery */
$('h1').text('World hello!');
$('body').append('<p>Lorem ipsum...</p>');
/* output the modified html with doctype */
console.log( window.document.doctype + window.document.innerHTML );
@fuddl
Copy link

fuddl commented Sep 8, 2014

/Applications/MAMP/htdocs/maprender/render.js:16
}).createWindow();
   ^
TypeError: Object [ null ] has no method 'createWindow'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment