Can we hide the javascript comment on the page when it displays?

the question may seem silly, it's just interesting to me. Suppose in my html or aspx page I added a comment to some javascript code, when the page loads, it appears as a page containing comments. as I thought the comment would be ignored from execution. Does this apply to rendering? this will be possible.

  • Comment is used to describe the code. therefore, below I wrote "this is for replacing the image." we can hide from rendering.

PS: Since this question has no special reasons, I'm just curious. is it possible or not.

$(".SomeID").slice(5, ExpandLength).each(function() { this.src = this.src.replace("some.gif", "som1.gif"); // this for replacing image }); 
+7
source share
9 answers

Take a look at YUI to minimize your javascript. When deploying to remove comments, you will need to do some post-processing using such a tool. Otherwise, you just stick to the same file, and people can see what you are doing (just like html or something else that is provided directly to the user).

Take a look at this question for a good answer:

YUI remove javascript comments

PS I assume your javascript is in a .js or .html file, otherwise (if in asp, aspx or php file) other answers about using server side comments is the best way to do this.

However, your javascript should be in the js file. It is much easier to manage :)

+3
source

External comments (JS, HTML, CSS, etc.) cannot be hidden from the visualization tool, but .net comments, for example. <%-- --%> not displayed in the source

0
source

It's good to comment on the code, but when javascript is used in production, it should be as compact as possible. Your best bet is to minimize the code for production. Search for "minify java script" on the Internet.

0
source

You will need to manually convert javascript using an external library, possibly during build time. For example. Using the YUI Compressor Library.

http://developer.yahoo.com/yui/compressor/

Unfortunately, Javascript cannot modify itself, so in JS there is no built-in way to remove comments from Javascript without an external library.

0
source

Rendering is a term that applies to two different stages of a process.

  • When ASP generates some output to send an HTTP response
  • When the browser parses HTML and creates a visible view for the user

JavaScript comment, as far as ASP is concerned, is another piece of text. He does not see the difference between this and a piece of HTML or any other part of JavaScript. Therefore, in step 1, it will be transferred to the browser.

When the browser receives a response, it processes HTML and JavaScript and creates a web page for viewing by the user. When it runs JavaScript, the comment has special meaning (i.e., it should be ignored), so it is not executed.

However, this is part of the source code.

To prevent you from having to run JavaScript through the JavaScript parser on the server, delete the comments, and then output all that remains.

A common practice is to save JavaScript in external files and process them using minifier (a tool that removes unnecessary text from JS, such as comments and spaces) before deploying the site to the server.

0
source

If you reduce the code using the build script, these comments will not be deleted, but your code will also not have spaces (except lines) and will display faster in DOM browsers. This is actually the best practice. Checkout

HTML 5 Boiler Plate Build Script, YUI Compress, or NetTuts Builder.

0
source

Several JavaScript minimization tools are available, including the Closure compiler, JSMin, or YUI Compressor.

You can create a build process that uses these tools to minimize and rename development files and save them in the production directory.

Experts recommend minimizing any JS files of size 4096 bytes or more.

You should see an advantage for any file that can be reduced by 25 bytes or more (less than this will not lead to a noticeable increase in performance).

Link: [http://code.google.com/speed/page-speed/docs/payload.html#MinifyJS.BIZ(Minimize payload size)

0
source

Pass your ASP code through this minifier to see if it solves your comment problems in JavaScript:

http://prettydiff.com/?m=minify&html

0
source

If you surround the comment block with script tags, they will not print on the screen

-one
source

All Articles