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?
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.
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.
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.
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.
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> ).
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With