Socket
Socket
Sign inDemoInstall

jsdom

Package Overview
Dependencies
74
Maintainers
3
Versions
258
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsdom

A JavaScript implementation of the DOM and HTML standards


Version published
Maintainers
3
Weekly downloads
21,317,318
decreased by-6.37%

Weekly downloads

Package description

What is jsdom?

The jsdom npm package is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. It is designed to create a web browsing environment similar to what you would find in a web browser, allowing for the parsing and manipulation of HTML and XML documents. It can be used for testing web pages and JavaScript libraries in a Node.js environment.

What are jsdom's main functionalities?

DOM Manipulation

This feature allows you to manipulate the DOM of a document. The code sample demonstrates how to append a new list item to an unordered list.

const { JSDOM } = require('jsdom');
const dom = new JSDOM(`<ul><li>Item 1</li></ul>`);
const ul = dom.window.document.querySelector('ul');
ul.appendChild(dom.window.document.createElement('li')).textContent = 'Item 2';
console.log(dom.window.document.body.innerHTML);

Event Handling

This feature enables you to simulate and handle events. The code sample shows how to simulate a click event on a button and handle it by logging a message.

const { JSDOM } = require('jsdom');
const dom = new JSDOM(`<button id='myButton'>Click me</button>`);
const button = dom.window.document.querySelector('#myButton');
button.addEventListener('click', () => console.log('Button clicked!'));
button.click();

Fetching Remote Content

This feature allows you to fetch remote content and create a JSDOM instance from it. The code sample demonstrates fetching and logging the HTML content of a webpage.

const { JSDOM } = require('jsdom');
JSDOM.fromURL('https://example.com/').then(dom => {
  console.log(dom.serialize());
});

Running Scripts

This feature allows you to run scripts within the context of the JSDOM instance. The code sample shows how to execute a script that changes the text content of the document body.

const { JSDOM } = require('jsdom');
const dom = new JSDOM(`<script>document.body.textContent = 'Hello, world!';</script>`);
console.log(dom.window.document.body.textContent);

Other packages similar to jsdom

Keywords

FAQs

Last updated on 01 May 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc