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 only 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?

Upvotes: 8

Views: 2785

Answers (6)

Erik Forbes
Erik Forbes

Reputation: 35871

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.

Upvotes: 12

Sani Huttunen
Sani Huttunen

Reputation: 24395

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.

Upvotes: 8

riwalk
riwalk

Reputation: 14233

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.

Upvotes: 2

Scott Evernden
Scott Evernden

Reputation: 39966

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

Upvotes: 2

Donald Miner
Donald Miner

Reputation: 39913

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

Upvotes: 4

will.i.am
will.i.am

Reputation: 374

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

Upvotes: 0

Related Questions