Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get reference to comment element/block by JavaScript?

This sounds a little crazy, but I'm wondering whether possible to get reference to comment element so that I can dynamically replace it other content with JavaScript.

<html> <head> </head> <body> <div id="header"></div> <div id="content"></div> <!-- sidebar place holder: some id--> </body> </html> 

In above page, can I get reference to the comment block and replace it with some content in local storage?

I know that I can have a div place holder. Just wondering whether it applies to comment block. Thanks.

like image 589
Morgan Cheng Avatar asked May 17 '11 07:05

Morgan Cheng


People also ask

How do you make a comment section in Javascript?

Single line Javascript comments start with two forward slashes (//). All text after the two forward slashes until the end of a line makes up a comment, even when there are forward slashes in the commented text.

How do you uncomment a block of code in Javascript?

Just add /**/ to the beginning and end of the block. Then add a space to the end of the top comment, /** / , to toggle the whole block.

How to get a reference to an HTML element with JavaScript?

One of the most common ways of getting a reference to an html element with javaScript is to use the document.getElementById method which is one of many methods to be aware of in the document object that is worth looking into in detail.

What is the use of comment block in HTML?

Block comments are often used for formal documentation. Using comments to prevent execution of code is suitable for code testing. Adding // in front of a code line changes the code lines from an executable line to a comment. document.getElementById("myP").innerHTML = "My first paragraph.";

How do I reference an HTML element in Blazor?

@ref and ElementReference When we require a reference to an HTML element we should decorate that element (or Blazor component) with @ref. We identify which member in our component will hold a reference to the HTML element by creating a member with the type ElementReference and identify it on the element using the @ref attribute.

How do I add a single line comment in JavaScript?

Single line comments start with //. Any text between // and the end of the line will be ignored by JavaScript (will not be executed). document.getElementById("myP").innerHTML = "My first paragraph.";


2 Answers

var findComments = function(el) {     var arr = [];     for(var i = 0; i < el.childNodes.length; i++) {         var node = el.childNodes[i];         if(node.nodeType === 8) {             arr.push(node);         } else {             arr.push.apply(arr, findComments(node));         }     }     return arr; };  var commentNodes = findComments(document);  // whatever you were going to do with the comment... console.log(commentNodes[0].nodeValue); 
like image 109
hyperslug Avatar answered Sep 29 '22 10:09

hyperslug


It seems there are legitimate (performance) concerns about using comments as placeholders - for one, there's no CSS selector that can match comment nodes, so you won't be able to query them with e.g. document.querySelectorAll(), which makes it both complex and slow to locate comment elements.

My question then was, is there another element I can place inline, that doesn't have any visible side-effects? I've seen some people using the <meta> tag, but I looked into that, and using that in <body> isn't valid markup.

So I settled on the <script> tag.

Use a custom type attribute, so it won't actually get executed as a script, and use data-attributes for any initialization data required by the script that's going to initialize your placeholders.

For example:

<script type="placeholder/foo" data-stuff="whatevs"></script> 

Then simply query those tags - e.g.:

document.querySelectorAll('script[type="placeholder/foo"]') 

Then replace them as needed - here's a plain DOM example.

Note that placeholder in this example isn't any defined "real" thing - you should replace that with e.g. vendor-name to make sure your type doesn't collide with anything "real".

like image 23
mindplay.dk Avatar answered Sep 29 '22 09:09

mindplay.dk