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?

4

2 に答える 2

2

次のようなことができますか?

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

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

VSは文句を言いません。

于 2013-03-12T16:46:35.777 に答える
0

javascript/html/css + server タグの組み合わせは、配置などに応じて波線が表示されます。

本当にそれを避けたい場合は、コード ビハインドで JavaScript を作成する必要があります。

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

そしてコードビハインドで。

if (Request.IsLocal) {
    script.Text = "<script>function(){}</script>";
}
于 2013-03-12T16:08:27.437 に答える