Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a DOM API for querying comment nodes?

I have a document with a debugging comment in it that looks like this:

<!--SERVER_TRACE {...}-->

Is there a way to query the DOM to access this node? I am looking for a vanilla JavaScript solution, without the aid of any libraries.

My first thought was to depth first search the DOM and compare nodes found against the node type value for comments, Node.COMMENT_NODE. Is there an easier way?

like image 851
Rick Viscomi Avatar asked Apr 22 '13 16:04

Rick Viscomi


People also ask

What does DOM API consists?

Introduction. The Document Object Model (DOM) is an application programming interface (API) for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated.

What kind of nodes does the DOM consist of?

Any lone text outside of an element is a text node, and an HTML comment is a comment node. In addition to these three node types, the document itself is a document node, which is the root of all other nodes. The DOM consists of a tree structure of nested nodes, which is often referred to as the DOM tree.

What are DOM and DOM nodes?

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.

What is a DOM tree node?

The Document Object Model (DOM) is an interface that treats HTML or XML document as a tree structure, where each node is an object of the document. DOM also provides a set of methods to query the tree, alter the structure, style.

What can I do with the HTML DOM API?

The functional areas included in the HTML DOM API include: Access to and control of HTML elements via the DOM. Access to and manipulation of form data. Interacting with the contents of 2D images and the context of an HTML <canvas>, for example to draw on top of them. Management of media connected to the HTML media elements ( <audio> and <video> ).

What is each node in the Dom?

Each node is based on the Node interface, which provides properties for getting information about the node as well as methods for creating, deleting, and organizing nodes within the DOM. Nodes don't have any concept of including the content that is actually displayed in the document.

What is a node in HTML?

Each node is based on the Node interface, which provides properties for getting information about the node as well as methods for creating, deleting, and organizing nodes within the DOM. Nodes don't have any concept of including the content that is actually displayed in the document. They're empty vessels.

What is the difference between Dom and document interface?

While the Document interface is defined as part of the DOM specification, the HTML specification significantly enhances it to add information specific to using the DOM in the context of a web browser, as well as to using it to represent HTML documents specifically. Among the things added to Document by the HTML standard are:


1 Answers

There's the TreeWalker APIs:

var tw = document.createTreeWalker(document, NodeFilter.SHOW_COMMENT, null, null),
    comment;
while (comment = tw.nextNode()) {
    // ...
}

This isn't supported by IE8 and lower.

T.J. in the comments provided a link to the specs. I kind of always use just TreeWalkers, but in your case a NodeIterator is fine too.

like image 112
MaxArt Avatar answered Oct 12 '22 16:10

MaxArt