how to use document.getElementById() in Nodejs
Asked Answered
M

2

5

i'm trying to get elements by id from an html file in a js file using nodejs. I'm getting the error 'document id not defined' because node doesn't provide a document object model by default.

So how can i use document.getElementById() in nodejs ?

Thank you !

Marinna answered 10/9, 2018 at 11:25 Comment(2)
node.js programs don't run in a browser. There is no document in the first place. Are you asking how to parse an HTML file in node.js?Terribly
Possible duplicate of How do I parse a HTML page with Node.jsTerribly
D
5

If you want to parse files within the same server you should probably use some of this options, because nodejs it's just a JavaScript implementation, There is no window or document object, see this. But to answer exactly your question:

how can I use document.getElementById() in nodejs ?

You could do it using Puppeteer

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

Here a basic example:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();


        page = await browser.newPage();
        await page.goto('http://example.com/some.html', {waitUntil: 'load'});


    const newPage = await page.evaluate(() => {

        return  document.getElementById("idexample").innerHTML;

        });

     console.log(newPage)

  })();
Dolerite answered 10/9, 2018 at 12:15 Comment(4)
OP wants to parse an HTML file on the server, not some website.Terribly
@ChrisG You could parse the files of the same server, also the question is: "how to use document.getElementById() in Nodejs" and this is a way to do that.Dolerite
The question doesn't mention there's a server running. And it clearly also says from an html file, which means this is almost an XY problem and using document.getElementById() was already a wrong alley in the first place. On top of that, the question is a clear duplicate, and answered extensively.Terribly
@ChrisG yes, it's probably an XY problem, I've made a clarification in my answer. Either way if somebody find this question googling how to use document.getElementById() in node, this is one possible correct answer. Thanks for the feedback!Dolerite
D
2

Use JSDOM npm package:

jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. In general, the goal of the project is to emulate enough of a subset of a web browser to be useful for testing and scraping real-world web applications. https://github.com/jsdom/jsdom

Dearden answered 10/9, 2018 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.