Shane
Shane

Reputation: 1075

How do I know which features are in V8?

I'm a web developer, so my first port of call for any Javascript documentation is usually MDN. For instance, right now I'd like to use the DOMParser, which does have support in Chrome.

However, when I'm running JavaScript through V8 (either Node or rubyracer), DOMParser isn't found.

~/$ node
Welcome to Node.js v12.4.0.
Type ".help" for more information.
> DOMParser;
Thrown:
ReferenceError: DOMParser is not defined
>

Is there a subset of JavaScript which only works in browsers? How do I find out which features V8 supports?

Upvotes: 0

Views: 474

Answers (2)

jmrk
jmrk

Reputation: 40561

The official, definitive answer is: when it's part of the ECMAScript specification, then it's implemented/supported by ECMAScript engines (V8, SpiderMonkey, JSC). Everything else you find in MDN (for example, anything DOM and network related) is specific to the browser environment, and implemented by the respective browser.

For an easier reading, MDN's JavaScript reference also seems to maintain that separation.

Upvotes: 3

Mohammad Reza Rahimi
Mohammad Reza Rahimi

Reputation: 662

Most of the web developer's think that any accessible object, prototype, and method in javascript that used in web development are pure javascript features while this idea is not right actually. some objects in web development such as XMLHttpRequest, DOMParser and etc are part of Document Object Model(DOM) that we can't access them except on browser platform.

If you are using Nodejs and you want to parse html text I think that Jsdom library satisfy your requirement as follow:

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log(dom.window.document.querySelector("p").textContent);

Online Execution

I hope this will help you

Upvotes: 3

Related Questions