Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping a double-quote in inline c# script within javascript

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.

like image 201
ajbeaven Avatar asked Sep 21 '10 23:09

ajbeaven


People also ask

How do I escape a double quote in Windows command line?

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.

How do you skip double quotes?

Double quotes characters can be escaped with backslash( \ ) in java.

How do you escape quotes within a quote?

Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.

How do you escape quotation marks in a string?

' 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.


1 Answers

Call HttpUtility.JavaScriptStringEncode.
This method is new to ASP.Net 4.0; for earlier versions, use the WPL.

like image 103
SLaks Avatar answered Sep 22 '22 12:09

SLaks