Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Server-Side Comments inside <script> Block

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.

like image 649
Alex Avatar asked Sep 22 '09 18:09

Alex


4 Answers

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.

like image 142
Toby Avatar answered Oct 13 '22 09:10

Toby


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.

like image 20
womp Avatar answered Oct 13 '22 11:10

womp


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

like image 24
Rap Avatar answered Oct 13 '22 09:10

Rap


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.

like image 22
AndreasN Avatar answered Oct 13 '22 10:10

AndreasN