简体   繁体   English

如何隐藏HTML注释

[英]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. 我想在HTML中添加一些注释作为对自己的注释,但我不希望网站上的某人能够看到我在开发工具中留下的注释。 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? 我看到有一个Firebug扩展程序可以对此进行切换,所以也许这是不可能的? But I wanted to check and see for sure. 但是我想检查一下以确保。

If you change the file type to be a php file and put the comment inside 如果您将文件类型更改为php文件,然后在其中添加注释

'<?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. 这将对用户隐藏评论,并且由于它是服务器端语言,他们将无法查看评论。

There is no real way to do this outside of using a minifier tool. 除了使用缩小工具之外,没有真正的方法可以做到这一点。 check out: https://kangax.github.io/html-minifier/ 签出: https : //kangax.github.io/html-minifier/

Just one of many. 只是其中之一。 You can find great tools for javascript as well as HTML. 您可以找到适用于javascript和HTML的出色工具。

You can use JavaScript / jQuery to remove the comments on the DOM. 您可以使用JavaScript / jQuery删除DOM上的注释。

The Code / Example ( https://jsfiddle.net/cjqap37e/ ): 代码/示例( 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. 更好的解决方案是使用服务器端脚本来删除注释,因为使用JavaScript / jQuery会将注释发送给访问者。 So you have the possibility to use a template engine like Smarty or Twig . 因此,您可以使用诸如SmartyTwig的模板引擎。 Another way is to use a server side minifier to minify the HTML-Code on the server and send the result to the visitor. 另一种方法是使用服务器端压缩程序来缩小服务器上的HTML代码并将结果发送给访问者。

Yes, you can, as long as you have your website made with .NET, .PHP or alike, basically, server side. 是的,只要您使用.NET,.PHP或类似的网站(基本上是服务器端)制作网站,就可以。 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. HTML代码将呈现为没有注释,您可以在DEV环境中将其作为源代码看到。

Hope this helps :) 希望这可以帮助 :)

I was also thinking if you are running on php you can format comments like: 我也在想,如果您在php上运行,则可以设置如下格式的注释:

<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: 您唯一需要考虑的是,需要确保通过php解释器解析.htm或.html文件,请参见:

Parse HTML as PHP 将HTML解析为PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM