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]
# 1 Re: Passing a string to a javascript with quotes
Strings in .NET are immutable which means any method that you execute on them returns a new string and does not do any change in the original string. So when you call a method on a string, you should save the results somewhere. Your code above should look like this longFix = longFix.Replace("'", "\'");
Shuja Ali at 2007-11-9 11:53:52 >
# 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
Anti-Rich at 2007-11-9 11:54:54 >
# 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)");
andreasblixt at 2007-11-9 11:55:52 >
# 4 Re: Passing a string to a javascript with quotes
well andreas that seems to have fixed it (lol although i could have bloody sworn i tried that)... so thankyou very very very much for helping me, and i extend that same thanks to you shuja, both of your inputs have been greatly appreciated.


til we meet again! :)

regards
adam
Anti-Rich at 2007-11-9 11:56:48 >