Passing a string to a javascript with quotes
Hi All,
I am beating my head against a brick wall right now, because i am trying to pass a string to a javascript function called Tip, which will show a tooltip when hovering over an image. basically, if the string has any single quotes, it craps itself. i know how escape chars work (or i think i do) but it doesnt seem to make a difference:
imgTableImage1.Attributes.Add("onMouseOver", "Tip('<table border="1" cellspacing="5"><tr><th colspan="1" style="font-size:8pt;background:#ffcccc;">" + longFix + "</th></tr><tr><td style="font-size:8pt;background:#ffffff;">" + sFullText.ToString() + "</td></tr></table>',WIDTH, 200, FADEIN, 500, FADEOUT, 500)");
that is the snippet which adds the tooltip functionality, and longFix is where i have been attempting to put the 'fixed' string.
i have tried using:
longFix.Replace("'", "\'");
longFix.Replace("'", @"\'") //this one puts in a second slash after the first
longFix = Regex.Replace("[']", @"\'");
and i STILL am having no luck.. can anyone see where i am going wrong?
cheers
adam
[1267 byte] By [
Anti-Rich] at [2007-11-20 10:32:14]

# 2 Re: Passing a string to a javascript with quotes
Hi Shuja, and thanks for your reply!
the annoying thing is it STILL doesnt seem to work! i do NOT understand what is going on here...
this is the snippet:
string longFix;
longFix = newsAlert.LongDesc.Replace("'", "\'");
sFullText = sFullText + "<br> Click button for more details...";
imgTableImage1.Attributes.Add("onMouseOver", "Tip('<table border="1" cellspacing="5"><tr><th colspan="1" style="font-size:8pt;background:#ffcccc;">" + longFix + "</th></tr><tr><td style="font-size:8pt;background:#ffffff;">" + sFullText.ToString() + "</td></tr></table>',WIDTH, 200, FADEIN, 500, FADEOUT, 500)");
sorry, maybe i have missed something important (newsAlert is its own object, with a longDesc property).
cheers
adam
# 3 Re: Passing a string to a javascript with quotes
\ always escapes the character after it, even if it doesn't have to be escaped (it will give an error if there is an unrecognized escape sequence.) Therefore, replacing "'" with "\'" does nothing (it replaces "'" with "'".) Instead, do this:
longFix = newsAlert.LongDesc.Replace("'", "\\'");
Also, " is an XML entity defined for HTML which represents a double quote. It's used for HTML values, not for XML elements (such as string delimiters), nor JavaScript, so use \" instead of ":
imgTableImage1.Attributes.Add("onMouseOver", "Tip('<table border=\"1\" cellspacing=\"5\"><tr><th colspan=\"1\" style=\"font-size:8pt;background:#ffcccc;\">" + longFix + "</th></tr><tr><td style=\"font-size:8pt;background:#ffffff;\">" + sFullText.ToString() + "</td></tr></table>',WIDTH, 200, FADEIN, 500, FADEOUT, 500)");