2011-05-02 99 views
5

我只是遇到了javascript问题,我使用的是asp.net上的代码,经过几个小时的计算后,成为逃避角色的问题。为什么在字符串中使用javascript转义字符的引号需要是\'而不是'

起初我用这个。

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can't delete this data because it is bound with rate plan');", true); 

这将使javascript错误,因为在“不能”的引用需要使用转义字符,所以我使用。

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\'t delete this data because it is bound with rate plan');", true); 

但它仍然不起作用。

最后我用

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert('Can\\'t delete this data because it is bound with rate plan');", true); 

,这是罚款。

我只是好奇,为什么我们需要使用\\'而不是\'为了使转义字符正常工作。

回答

7

\是C#中的转义字符在JavaScript中。

当你给C#"\'"创建一个包含撇号的字符串。

当你给C#"\\'"则第一\转义第二\(因此第二\不被视为转义字符)和'作为处理过的纯'(因为字符串不与'分隔。

+0

谢谢您的详细解释,我会在这个问题上更加小心。 – 2011-05-02 10:03:37

1

当您使用\\它逃到\其中转义字符实际的JavaScript。你基本上是两次逃脱

+0

感谢您的回答垫子:D – 2011-05-02 10:13:22

3

在AC#小号特林,\需要进行转义,因为它是对于像\n等特殊前缀您可能会发现它更容易使用一个逐字直译STRIG,这并不需要转义(除了""")。

例如:

@"... can\'t ..." 

注领先@之前字符串文字,表示替代转义规则的使用。这也允许直接在字符串中换行等,即

@"foo 
bar 
blip" 
+0

谢谢Marc Gravell。 – 2011-05-02 10:05:31

3

因为“\”也是C#的转义字符。

在开始字符串之前,我倾向于在字符串的开始处使用@特殊运算符,因为它告诉C#它不能处理字符转义。

例如:

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", @"alert('Can\'t delete this data because it is bound with rate plan');", true); 

反正我没有找到一个单一的QUOT点。您可以通过避免使用双QUOT串符号逃脱这个单一QUOT:

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "alert(\"Can't delete this data because it is bound with rate plan\");", true); 

我不明白,单QUOT的JavaScript中的滥用,如果我不记得有正在很多PHP程序员的贡献脚本,因为这种语言的行为依赖于单引号或双引号字符串。

无论如何,你可以检查这个问题,其它单,双引号在JavaScript:在名称

+0

谢谢你,我对单引号和双引号没有太多的想法,谢谢你指出这个问题,所以下次我可以更好地使用它:) – 2011-05-02 10:07:24

+0

当然,我发现一个坏方法使用其他的“优点” JavaScript中的语言或任何其他的语言,如果这些是无用的:)很高兴知道它是有用的。 – 2011-05-02 10:11:49

0

单引号和撇号(如奥布莱恩)通常在动态客户端脚本中造成麻烦,因为它们会破坏它们并允许插入恶意代码(又称脚本攻击)。

我已经写以下C#6扩展方法用于代码隐藏来解决这个:

public static class Extension 
{ 
    public static string ToSQEscapedStringJS<T>(this T unescapedStr) 
    { 
     if (unescapedStr == null || unescapedStr.ToString() == "") 
     { 
      return "''"; 
     } 
     // replace ' by @@@ 
     var escapedStr = (unescapedStr).ToString().Replace("'", "@@@"); 
     // JS code to replace @@@ by ' 
     string unEscapeSQuote = "replace(/@{3}/g, String.fromCharCode(0x27))"; 
     // add @@@ escaped string with conversion back to ' 
     return $"('{escapedStr}'.{unEscapeSQuote})"; 
    } 
} 

它的使用很简单。请看下面的动态脚本例子:

// contains apostroph (aka single quote) and is dangerous for your script block 
var name = "O'Brian"; 
var nameEscp = name.ToSQEscapedStringJS(); // creates JS expression from it 
// building up the script 
string strScript = 
    $"<script>window.opener.document.forms(0).{control.Value}.value = {nameEscp};</script>"; 
ClientScript.RegisterClientScriptBlock(this.GetType(), "anything", strScript); 

注意nameEscp已经由单引号包围,所以你可以放心地把它放在=后。

诀窍在于,该字符串被转义并且在分配立即未转义(通过执行JavaScript表达式)上飞,即

.value = ('[email protected]@@Brian'.replace(/@{3}/g, String.fromCharCode(0x27)); 

将是将被发送到客户端所插入赋值表达式脚本。执行后,.value包含O'Brian