最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Sep 22, 2009 at 18:51 AlexAlex 77.3k91 gold badges264 silver badges350 bronze badges 2
  • 2 Use regular comments and minify your Javascript upon deployment – Josh Stodola Commented Sep 22, 2009 at 19:00
  • 2 Can I minify javascript that's embedded in an ASPX file automated on deployment? – Alex Commented Sep 22, 2009 at 19:01
Add a comment  | 

5 Answers 5

Reset to default 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.

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.

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论