Jon Spalding
Jon Spalding

Reputation: 67

How to Hide HTML Comments

I want to add some comments as notes to myself in my HTML, but I don't want someone at my website to be able to see the comments I left in dev tools. Are there any tricks to have comments seen in code, but not accessible in dev tools? And I don't mean an option to toggle it on or off, but as far as the user is concerned, I don't want them to ever know the comments are there.

I saw there's a Firebug extension to toggle this, so maybe this just isn't possible? But I wanted to check and see for sure.

Upvotes: 1

Views: 8187

Answers (5)

user10660932
user10660932

Reputation:

If you change the file type to be a php file and put the comment inside

'<?php /* comments */ ?>'

this will hide the comments from the users and they will not have the ability to see it because it's a server side language.

Upvotes: 2

James Bailey
James Bailey

Reputation: 508

I was also thinking if you are running on php you can format comments like:

<br>
Html stuff...
<tag></tag>...... <?php //This is a comment that is hidden /?>

The only thing you have to take into account it that you need to make sure that your .htm or .html files are parsed through a php interpreter see:

Parse HTML as PHP

Upvotes: 0

Sebastian Brosch
Sebastian Brosch

Reputation: 43574

You can use JavaScript / jQuery to remove the comments on the DOM.

The Code / Example (https://jsfiddle.net/cjqap37e/):

$('*').contents().each(function() {
  if(this.nodeType === Node.COMMENT_NODE) {
    $(this).remove();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<!-- comment #1 -->
<div class="test">
  Hello World
  <!-- comment #2 -->
</div>

The visitor of the site get the following output:

<div class="test">
    Hello World
</div>

A better solution would be to use a server side script to remove the comments because with JavaScript / jQuery the comments are sent to the visitor. So you have the possibility to use a template engine like Smarty or Twig. Another way is to use a server side minifier to minify the HTML-Code on the server and send the result to the visitor.

Upvotes: 1

SG1Asgard
SG1Asgard

Reputation: 64

Yes, you can, as long as you have your website made with .NET, .PHP or alike, basically, server side. You might have something as such:

<?php
    echo 'This is a test'; // This is a one-line c++ style comment
    /* This is a multi line comment
       yet another line of comment */
    echo 'This is yet another test';
    echo 'One Final Test'; # This is a one-line shell-style comment
?>

HTML code will be rendered without the comments which you will be able to see in DEV environment as source code.

Hope this helps :)

Upvotes: 0

James Bailey
James Bailey

Reputation: 508

There is no real way to do this outside of using a minifier tool. check out: https://kangax.github.io/html-minifier/

Just one of many. You can find great tools for javascript as well as HTML.

Upvotes: 0

Related Questions