0

I got sick of all my jscript social widget/analytics initialization cluttering my debug output while testing my Asp.Net webforms page locally. So I tried this in my HTML markup:

<script>
if (<%= !Request.IsLocal ? "true" : "false" %>) {

This actually emits correct jscript code ("if (true)") with no run-time errors but Visual Studio doesn't like it. I'm getting a syntax error on the closing ')'. Definitely don't like seeing compile errors, even if they're bogus, every darn time I compile.

I changed it to compare strings, like

if ("<%= Request.IsLocal %>" === "False") {

Which works and emits code like 'if ("True" === "False")', but... that just grates on my nerves and looks ugly. Is this just a Visual Studio weirdness, or is there a better way to use asp.net server boolean values in jscript conditionals?

2 Answers 2

2

Could you do something like:

<script>
  var isLocal = "<%= Request.IsLocal %>" == "True";
  ...

  if(isLocal) {
    // do local stuff
  }
</script>

VS does not complain.

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

3 Comments

Yes, as per my question, this is the current solution I'm using. My question was whether there was a better way or whether this was just some VS intellisense weirdness.
VS intellisense is just confused because you are mixing JavaScript and ASP.NET code. When you put ASP.NET code within quotes, intellisense considers it a JavaScript string, but compiler correctly replaces the value within angle brackets <%= %>, so there is no "complaints" in VS. My suggestion was about isolating this "ugliness" on top of your JavaScript code. The other way to do this would be to insert JS code from you code behind, but I think that is far less straight forward.
Yeah that seems to make the most sense in this situation, thanks.
0

The combination of javascript/html/css + server tags will show squigly lines depending on their placement etc.

If you truly want to avoid that, you would have to create your javascript in the code behind.

<asp:Literal id="script" runat="server">

And in the code behind.

if (Request.IsLocal) {
    script.Text = "<script>function(){}</script>";
}

1 Comment

This is interesting, might work well for very small scripts, but... you lose intellisense and all the usual editor goodness since VS just sees your jscript code as a string, not code... doesn't seems like a worthy trade-off to me, but thanks for the interesting option.

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.