开发者

Javascript: Why if(false)?

I saw this in code. It blew my mind.

<% if (false) { %>
<script type="text/javascript" src="~/Scripts/jquery-1.3.2.js"></script>    
<% } %>

This seems so patently illogical that it must be intentional. I can o开发者_如何转开发nly assume that somehow this "came up", and somebody inserted this as a work-around. There are, of course, no comments.

Why would someone do this?


That's a trick to get Visual Studio to include the javascript Intellisense for jQuery without actually emitting the script to callers.

Here is an example from Scott Gu explaining it.


Intellisense in Visual Studio works for jQuery if you add that to every .aspx, .ascx file.
But instead of including it in every file it is included only in the masterpage. Visual Studio parses the markup files and finds a reference to jQuery and then uses the provided intellisense on it.

You'll also need to add a vsdocs.js file into the project.
You can read more about it here.


if(false) is a quick and dirty way to comment out a bunch of code


is there another line of code that looks like

<script type="text/javascript" src="~/Scripts/jquery-min.1.3.2.js"></script>  

or similar? My guess is whoever wrote this wanted to have an easy way to switch in the big jQuery file for debugging purposes


To comment out code.

ASP does not respect HTML comments, so some people will use this, not knowing that ASP has its own syntax for comments.


This is just like a comment, to do not execute the script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜