2011-05-02 39 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

相關問題