I need to escape a double quote in inline c# within javascript. Code is below:
if ("<%= TempData["Message"]%>" == "") {
// code
};
Normally, I would just use single quotes like so:
if ('<%= TempData["Message"]%>' == "") {
// code
};
However, TempData["Message"]
has single quotes within it (when it contains a link generated by the Html.ActionLink() helper in ASP.NET MVC). So while I could change all the ActionLink helpers inside TempData["Message"]
to tags, it's an interesting problem and would been keen to hear if anyone has an answer.
Escape every double quote " with a caret ^ . If you want other characters with special meaning to the Windows command shell (e.g., < , > , | , & ) to be interpreted as regular characters instead, then escape them with a caret, too.
Double quotes characters can be escaped with backslash( \ ) in java.
Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.
' You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.
Call HttpUtility.JavaScriptStringEncode
.
This method is new to ASP.Net 4.0; for earlier versions, use the WPL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With