Buy DarkLaunch.com

Dark Launch

Parse html string with Node.js, jQuery, and jsdom

Parse an html string using Node.js, jQuery, and jsdom.

Install jsdom and jquery.

Javascript
$ npm install jsdom
$ npm install jquery

Write JavaScript file.

Javascript
var jsdom = require('jsdom');
var dom = new jsdom.JSDOM();
var window = dom.window;
var document = window.document;
 
var $ = require('jquery')(window);
console.log('version:', $.fn.jquery);
 
var $content = $('<p><a href="https://www.example.com/">Link</a></p>');
console.log('text:', $content.text());
console.log('html:', $content.html());
console.log('href:', $content.find('a').attr('href'));

Run JavaScript file.

Bash
$ node script.js
version: 3.2.1
text: Link
html: <a href="https://www.example.com/">Link</a>
href: https://www.example.com/