By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

string with quotes

P: 27
Hi,
Below is my javascript string which i will be using in asp.net

Expand|Select|Wrap|Line Numbers
  1.  sTemp = "<script language=javascript>;parent.document.getElementById('ctl00$ContentPlaceHolder1$txt_name').value ='" + myname + " ' ;</script>"
  2. ClientScript.RegisterStartupScript(Me.GetType, "myscript", sTemp)

variable myname is having single quotes values (myname =John O'Loughlin
) .I get javascript error (Expected ';') when i register the script

any solution to assign value with variables having single quotes?

Thanks
Mpl
Jul 30 '08 #1
Share this Question
Share on Google+
1 Reply


rohypnol
P: 54
Hello,

This is more of a .NET question... To paste the contents of the variable you should escape the quotes with a backslash. Replace every quote with backslash+quote.
But keep in mind, this is still dangerous so please check this http://www.google.com/search?hl=en&q...ng&btnG=Search The first link it returns is http://www.west-wind.com/weblog/posts/114530.aspx which I've found to be very helpful for what you need.
Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2. /// Encodes a string to be represented as a string literal. The format
  3. /// is essentially a JSON string.
  4. /// 
  5. /// The string returned includes outer quotes 
  6. /// Example Output: "Hello \"Rick\"!\r\nRock on"
  7. /// </summary>
  8. /// <param name="s"></param>
  9. /// <returns></returns>
  10. public static string EncodeJsString(string s)
  11. {
  12.     StringBuilder sb = new StringBuilder();
  13.     sb.Append("\"");
  14.     foreach (char c in s)
  15.     {
  16.         switch (c)
  17.         {
  18.             case '\"':
  19.                 sb.Append("\\\"");
  20.                 break;
  21.             case '\\':
  22.                 sb.Append("\\\\");
  23.                 break;
  24.             case '\b':
  25.                 sb.Append("\\b");
  26.                 break;
  27.             case '\f':
  28.                 sb.Append("\\f");
  29.                 break;
  30.             case '\n':
  31.                 sb.Append("\\n");
  32.                 break;
  33.             case '\r':
  34.                 sb.Append("\\r");
  35.                 break;
  36.             case '\t':
  37.                 sb.Append("\\t");
  38.                 break;
  39.             default:
  40.                 int i = (int)c;
  41.                 if (i < 32 || i > 127)
  42.                 {
  43.                     sb.AppendFormat("\\u{0:X04}", i);
  44.                 }
  45.                 else
  46.                 {
  47.                     sb.Append(c);
  48.                 }
  49.                 break;
  50.         }
  51.     }
  52.     sb.Append("\"");
  53.  
  54.     return sb.ToString();
  55. }
Regards,
Tom
Jul 30 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.