简体   繁体   English

在javascript条件下使用asp.net内联标签

[英]Using asp.net inline tags in javascript conditional

I got sick of all my jscript social widget/analytics initialization cluttering my debug output while testing my Asp.Net webforms page locally. 在本地测试我的Asp.Net webforms页面时,我厌倦了所有我的jscript社交小部件/分析初始化混乱我的调试输出。 So I tried this in my HTML markup: 所以我在我的HTML标记中尝试了这个:

<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. 这实际上发出了正确的jscript代码(“if(true)”),没有运行时错误,但Visual Studio不喜欢它。 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. 哪个工作并发出像'if(“True”===“False”)'这样的代码,但是......这只是让我神经紧张并且看起来很难看。 Is this just a Visual Studio weirdness, or is there a better way to use asp.net server boolean values in jscript conditionals? 这只是一个Visual Studio的怪异,还是有更好的方法在jscript条件中使用asp.net服务器布尔值?

Could you do something like: 你可以这样做:

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

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

VS does not complain. VS不抱怨。

The combination of javascript/html/css + server tags will show squigly lines depending on their placement etc. javascript / html / css +服务器标签的组合将根据其位置等显示斜线。

If you truly want to avoid that, you would have to create your javascript in the code behind. 如果你真的想避免这种情况,你必须在后面的代码中创建你的javascript。

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

And in the code behind. 并在代码背后。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM