• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

Want comments in my JavaScript, but not have them appear with view source

Hi,

I'd like to keep browsers from reading our JavaScript comments. I DON'T WANT TO REMOVE THE COMMENTS (e.g, with minify) -- they're in the code for a reason.

Server-side comments using <%--  --%> perform this task well for HTML markup, but generate compiler warnings inside of Visual Studio when used with JavaScript. Is there a "proper" method of hiding JavaScript comments from browser users.

To repeat, we want to keep the JavaScript comments inside the code, for our own purposes, but we'd prefer that OTHERS only view the code, NOT the associated comments.

Thanks in advance.
0
paragram
Asked:
paragram
  • 6
  • 6
  • 3
1 Solution
 
Heritage02RiderCommented:
JavaScript by its very nature is not capable of doing this. It is a client side application run at the client on the clients web browser. You cannot completely hide it nor can you selectively hide comments and other items.

You're only option is to have two sets of code; the one in production and the one you use for development and testing. You must obfuscate the production to remove the unwanted comments and leave available in the development and testing.
0
 
Heritage02RiderCommented:
BTW - confuscation is merely a blanket over the code. There are decryption sites available to un-obfuscate javascript. If you need secrecy your need to use something other than javascript.
0
 
GaryCommented:
What platform are you using, there are options to do minification at runtime e.g.
http://verens.com/2008/05/20/efficient-js-minification-using-php/

But you have to ask yourself is it worth the effort, Heritage02Rider's suggestion is the easiest
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
paragramAuthor Commented:
Heritage..., gary... --

Sorry, I didn't mean this would be something done on the client. Obviously that's neither desirable nor possible. I am talking about the Visual Studio server performing this feat. It can do it with HTML tag markup, but for some reason generates compiler warnings when inside of JavaScript. Should I have posted this question to the V/S area?

Thanks again.

Paragram
0
 
Heritage02RiderCommented:
The possibility exists where you could create a server-side script to generate the javascript script on the fly, but as Gary says 'Is it worth the effort?'

I have done this for one reason or another a long time ago using ASP, but never found a need with ASP.NET, so have not tried.
0
 
paragramAuthor Commented:
GaryC123 -- great! I'm using 4.5. Very excited to learn this is built in. But don't tease me! What is the syntax??

Heritage02Rider -- no offense, but if you haven't tried, pray tell, why are you posting here? I don't mean to be rude to someone who is trying to help, but I can't help but wonder, what are you adding to the conversation?

Gary -- Since Heritage mentions your response (which I didn't want to comment on, for fear of sounding rude) but it is a teensy bit insulting to say "is it worth the effort," do not you think? Obviously I do think it is worth the effort, or I wouldn't be asking. You mention minification, but I believe I gently stated in my opening sentence that I was not interested in minification. Really, we all come in here with our own hopes and dreams and quirks. Mine is that I don't choose to do minification. I WANT my comments to appear, QUA COMMENTS, to our developers. But just filtered out by the compiler. I don't want to remove them. I put that up front. So please, don't be offended, but also, please don't suggest a solution involving minification. I have found the helpers on this board to be a bit thin skinned. I know you guys are offering help "for free" but I am making a payment every month, so for me it isn't free. Just cut me a tad bit o' slack and let me be direct w/o taking offense. You know, posting here is sometimes difficult to be frank w/o sounding overly agressive. I apologize in advance. Thank you.

But hey, I am still hoping you will respond with that syntax for doing this, server-side, using 4.5. Thanking you in advance, and sincerely appreciative...

Paragram
0
 
GaryCommented:
Minification is the process of removing comments etc. - I'm not talking about obfuscation which is something completely different.
Since you have .net 4.5 then it is worth the effort since you only have to add one line.  If you didn't then it may have been just easier to keep a commented back up copy
If your js in a folder called scripts then all you need to add to the page is
<script src="scripts/js"></script>
0
 
paragramAuthor Commented:
Thank you, Gary. But I cannot get this to work. I wonder if I truly understand your instructions. Say my JavaScript is in a file called scripts/myJavaScript.js. Are you saying that inside of THIS file I add the line <script src="scripts/js"></script> and all of my "//" and "/*...*/" comments will be stripped off prior to the script being sent to the client browser? I must confess this doesn't seem to make much sense! But I tried it, and I'm sorry to say it doesn't seem to make any change whatsoever. I can still see the comments on the client (for example, by reading the JavaScript using developer tools on IE or Chrome.) You understand -- I want the comments REMOVED from the JavaScript BEFORE the script is shipped off to the browser. Sorry to be so dense here, but can you clarify what you are instructing me to do. [Note: the <%-- ... --%> syntax does what I want in HTML script, but generates a compiler warning when used inside of JavaScript script. I think all I'm asking is how do I avoid this warning?!]

Thanks.

Paragram
0
 
GaryCommented:
No - you place the script tag in your <head> section in the main aspx file - it replaces your current <script> calling tags for the javascript
Not sure what setup you are using but I'm sure you know where the head section is.
0
 
paragramAuthor Commented:
Gary,

Hmmm. Now I am REALLY confused. Yes! I know where the <head > is! But Gary -- our CURRENT calling tag is:

<script type="text/javascript" src="scripts/myJavaScript.js" />.

Are you saying if I replace this with

<script src="scripts/js"></script>

All of our comments will be removed prior to going to the browser? All I get is an error that "js" can be found. I know I'm being dumb here, or maybe just overly literal. What am I doing wrong?

Paragram
0
 
GaryCommented:
That should be it, are you sure the project is referencing .net 4.5 and not a previous version.
0
 
paragramAuthor Commented:
Hi Gary -- wow, that's cool. I wish I could get it to work! So the compiler simply looks for ALL javascript files in that path (/scripts/*.js) that are being included in the current project? I did not know about this feature. In fact, I have looked for it in the V/S docs and googled for it as well. It's not that I doubt you -- but can you send me a link to the documentation for this feature? What it is called?

If you can do that, I'll mark this solved even if I can't get it working myself. Thanks again.

Paragram
0
 
paragramAuthor Commented:
Hooray for GaryC123. He not only stuck with me to provide the solution, but he did it in an extremely professional manner.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now