Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript read html comment after closing html tag

I know you shouldn't put anything after the closing 'html' tag. Tell SharePoint about it...

[...]
</body>
</html><!-- Rendered using cache profile:Public Internet (Purely Anonymous) at: 2013-06-06T12:57:10 -->

This is what the SharePoint output caching debug information looks like. I want this hidden comment to be visible on every page. Switching to source view and going to the end of the file makes me tired.

In an effort to not reinvent the wheel I figured it would be the smartest choice to add a piece of javascript code to my masterpage which copies the comment to a location (within the page) of my choosing.

Any idea on how I get hold of the comment via javascript? jquery is ok.

like image 288
lapsus Avatar asked Jun 06 '13 11:06

lapsus


People also ask

What are the opening and closing tags for an HTML comment?

An HTML comment begins with <! –– and the comment closes with ––> . HTML comments are visible to anyone that views the page source code, but are not rendered when the HTML document is rendered by a browser.

What is the ending tag for comments in HTML?

The HTML Comment Tag Comments in HTML start with <! -- and end with --> .

How do you comment out a tag in HTML?

To “comment out” in HTML, simply place the < ! ╌ ╌> tags around the code you want to hide. These tags will tell browsers not to render this code on the front end. Commenting out has two main purposes.

What is the use of HTML comment in JavaScript?

The comment tag is used to insert comments in the source code. Comments are not displayed in the browsers. You can use comments to explain your code, which can help you when you edit the source code at a later date.


2 Answers

You can get the nodeValue of the Comment Object and append it to Body Element:

$(document).ready(function() {
   var comment = $('html').prop('nextSibling').nodeValue;
   $('<div/>').html(comment).appendTo('body');
});

http://jsbin.com/arodiz/2/edit

like image 118
undefined Avatar answered Sep 29 '22 08:09

undefined


Simply document.lastChild.nodeValue will do the trick.

(Assuming you run it after the DOM is ready)

edit

I took the liberty of modifying the code from undefined's answer :)

$(function(){
    $('body').append(document.lastChild.nodeValue);
});

http://jsbin.com/arodiz/3/edit

like image 35
xec Avatar answered Sep 29 '22 08:09

xec