ToolTip Text: Single Quotes Should Be Replaced By '

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
Bob Martin
Advanced
Advanced
Posts: 26
Joined: Tue Nov 18, 2003 9:54 pm

ToolTip Text: Single Quotes Should Be Replaced By '

Post by Bob Martin »

Some text which I have been using for tool tip text (from an external database) using onfunction=showtip() has single quotes it in, which in turn causes a Javascript error within the Milonic runtime when the mouse hovers above that menu item.

My workaround, which I think should be part of your menu item build process, should be to replace any single quotes with the HTML equivalent, as in :

Code: Select all

tooltip.replace(/'/g, "&#xx;"); <!-- where xx is 39 for a single quote -->
Thoughts?

Thanks in advance,

Bob Martin
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

The problem will occur if you pass a literal string to the showTip() function using single quotes around the string (showTip('like thisexample') ). In that case, the additional apostrophe in the string is interpreted as the end of the string, and then there's a bunch of other stuff folowing it that doesn't beong there. It gets harrier if you're passing it in a js call in the url attribute of a static link <a url="javascript:showTip('like ths example')">click me</a>, because people will ften enclose the url argument in double quotes, leaving single qutesas the only thing left in the call to showTip().

I'm rambling.... it's late.

Anyway.. this is a general problem with string handling in js, not something specific to the menu or to the tooltips add-on. Your suggestion of using an html code is a good one. I find that another approach would be to escape the single quote so that it is interpreted literally, as part of the string. "Escaping a quote" is accomplished by placing a backslash in front of it, like so: \'.

Code: Select all

showTip('A single quote as an apostrophes shouldn\'t be a problem here, because it\'s escaped.')
Cheers,

Kevin
perldev
Mega User
Mega User
Posts: 115
Joined: Thu Aug 26, 2004 5:23 pm
Location: Chicago

Post by perldev »

If you use perl, you have to escape \ first. For an example:

Code: Select all

print <<EOF;
<a href="showTip('This is perldev\\'s test.')">whatever</a>
EOF
Post Reply