Click to See Complete Forum and Search --> : Passing a string to a javascript with quotes


Anti-Rich
September 5th, 2007, 02:17 AM
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=&quot;1&quot; cellspacing=&quot;5&quot;><tr><th colspan=&quot;1&quot; style=&quot;font-size:8pt;background:#ffcccc;&quot;>" + longFix + "</th></tr><tr><td style=&quot;font-size:8pt;background:#ffffff;&quot;>" + 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

Shuja Ali
September 5th, 2007, 02:46 AM
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("'", "\'");

Anti-Rich
September 5th, 2007, 02:51 AM
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=&quot;1&quot; cellspacing=&quot;5&quot;><tr><th colspan=&quot;1&quot; style=&quot;font-size:8pt;background:#ffcccc;&quot;>" + longFix + "</th></tr><tr><td style=&quot;font-size:8pt;background:#ffffff;&quot;>" + 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

andreasblixt
September 5th, 2007, 03:06 AM
\ 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, &quot; 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 &quot;:
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)");

Anti-Rich
September 5th, 2007, 03:24 AM
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