8

I know that you can create 'server-side comments' (they won't be sent as comments/text to the client) in ASP.NET (MVC) via the <%-- Comment --%> tags.

However, I can't seem to do this inside of a <script> tag -- if I try this I get a bunch of code underlined in red, and weird unrelated errors ("Invalid expression term '}') etc. from Visual Studio.

Is there another way to have server-side comments inside of the script tag? I want to comment my inline Javascript, but don't want my comments sent to the client.

2
  • 2
    Use regular comments and minify your Javascript upon deployment Commented Sep 22, 2009 at 19:00
  • 2
    Can I minify javascript that's embedded in an ASPX file automated on deployment? Commented Sep 22, 2009 at 19:01

5 Answers 5

10

You can add the comment no problem.

Visual Studio is stupid and doesn't recognize the ASP <%-- Comment %> tags in JS. Your page will still compile fine.

As mentioned in another answer, using //<%-- Comment %> will hide your comments (but leave the //).

Also, be careful of ASP.NET's habit of ignoring whitespace or line breaks around ASP-wrapped code:

//<%-- Comment %>
var whatever = '';

May become:

//var whatever = '';

at run time.

Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried commenting the lines with javascript comments as well? Apparently this should work:

<script type="text/javascript">
<%--
// Comments that
// will not be rendered
//--%>
</script>

Taken from a post on Scott Guthrie's blog here.

Comments

1

You can add comments in javascript by making each line start with "//". Those survive through the ASP.NET engine just fine.

1 Comment

I think the point is that he wants the comments to be removed once the page processing is done... i.e. he only wants to see the comments on the server side.
1

Server tags does work inside aspx javascript tags. But visualstudio doesn't get it, it gives you lots of errors, but if you run the page it will work.

Its the same if you do serverside inside a html tag.

Comments

1

Shouldn't you ideally be decoupling your JS code from ASPX as much as possible? The bulk of your JS code that is complicated enough to deserve commenting should reside in stand-alone JS files. You should have the bare minimum amount of code on the ASPX side and simply invoke JS functions etc. from the external JS files.

2 Comments

I'm doing that, but my document.ready() needs to stay in my ASPX file.
But the body of your document.ready() could be a one-line call to a function defined in a stand-alone file...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.