Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js inspecting dom like standard javascript

How is it possible to create document object from html source and use document.* functions like getElementById in node.js?

like image 447
Andrey Kuznetsov Avatar asked Aug 18 '10 15:08

Andrey Kuznetsov


2 Answers

You probably want something like the javascript implementation of the DOM, jsdom.

like image 179
meder omuraliev Avatar answered Oct 30 '22 16:10

meder omuraliev


If you just want to use a jQuery-like API to traverse and fiddle with HTML markup, a better option is cheerio.

jsdom is a full-fledged DOM implementation that can even run the JS that comes with a page. As a result, it's pretty heavy. If you don't need any of that functionality, cheerio is 8x faster.

var cheerio = require('cheerio'),
    $ = cheerio.load('<h2 class="title">Hello world</h2>');

$('h2.title').text('Hello there!');
$('h2').addClass('welcome');

$.html();
//=> <h2 class="title welcome">Hello there!</h2>
like image 30
josh3736 Avatar answered Oct 30 '22 17:10

josh3736