Ravi Gadag
Ravi Gadag

Reputation: 15881

can we hide javascript comment in page when it renders ?

question may seems to be silly, just i am curious about this. suppose in my html or aspx page i added comment for some javascript code, when the page loads, it renders as the page it is inlcuding comments. as i thought that comment will be ignored from the execution. is it same apply for rendering ? will it be possible.

  1. Comment are used to describe what the code does. so below i wrote " this for replacing image ". can we hide from rendering.

P.S : As this question has no special reason, just i am curious . whether it is possible or not.

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

Upvotes: 8

Views: 6800

Answers (10)

FredyWenger
FredyWenger

Reputation: 2325

I work with ASP.net core, have moved the JS to site.js and use minifying.
But I still have some JS in the view and don't want, that my comments are showed in GC.
In Asp.net core, you can use:

@* Your hidden comment *@
instead of
// Your visible comment

Upvotes: 0

Peter
Peter

Reputation: 1

If you surround a block of comments with script tags they will not print to the screen

Upvotes: -2

austincheney
austincheney

Reputation: 1199

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

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

Upvotes: 0

antonjs
antonjs

Reputation: 14338

Several tools are freely available to minify JavaScript, including the Closure Compiler, JSMin or the YUI Compressor.

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

The experts recommend minifying any JS files that are 4096 bytes or larger in size.

You should see a benefit for any file that can be reduced by 25 bytes or more (less than this will not result in any appreciable performance gain).

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

Upvotes: 0

jjNford
jjNford

Reputation: 5270

If you minify the code with a build script not only will these comments be removed, but your code will also have no white-spaces (other than in strings) and will render faster in the browsers DOM. This is actually best practice. Checkout

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

Upvotes: 0

Quentin
Quentin

Reputation: 944568

Rendering is a term that is applied to two distinct stages of the process.

  1. When the ASP generates some output to send in the HTTP response
  2. When the browser parses the HTML and creates a visible representation for the user

A comment in JavaScript is, as far as the ASP is concerned, just another piece of text. It sees no difference between that and a piece of HTML or any other part of the JavaScript. Therefore, during stage 1, it will be passed through to the browser.

When the browser receives the response it processes the HTML and JavaScript and generates the webpage for the user to see. When it is executing the JavaScript the comment has special significance (i.e. it should be ignored) so it isn't executed.

It is still part of the source code though.

To prevent that you would have to run the JavaScript through a JavaScript parser on the server, remove the comments, then output whatever remained.

It is a common practise to keep JavaScript in external files and to process them using a minifier (a tool that removes unneeded text from the JS, such as comments and white space) before deploying the site to the server.

Upvotes: 1

reach4thelasers
reach4thelasers

Reputation: 26919

You'd need to manually transform the javascript using an external library, probably at build time. E.g. Using the YUI Compressor library.

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

Unfortunately Javascript can't modify itself so there's no inherent way in JS to remove comments from the Javascript without an external library.

Upvotes: 0

erict
erict

Reputation: 1467

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

Upvotes: 0

Gats
Gats

Reputation: 3462

Look at YUI to minimise your javascript. You will need to do some post-processing using a tool like that when you deploy to remove comments. Otherwise, you're just sticking the same file up and people can see what you do (just like html or anything else that is served to the user directly).

Look at this question for a good answer:

YUI remove javascript comments

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

Still.. your javascript should be in a js file. It's much easier to manage :)

Upvotes: 3

danwellman
danwellman

Reputation: 9273

Front-end comments (JS, HTML, CSS etc) cannot be hidden from the renderer, but .net comments, e.g. <%-- --%> are not rendered in the source

Upvotes: 0

Related Questions