Connecting Tech Pros Worldwide Forums | Help | Site Map

Nested quotation marks/single quotes problem

Dustin
Guest
 
Posts: n/a
#1: Aug 13 '06
I have a nested quotes issue I'm trying to resolve. Basically I need a 5th
level nested quote for quoting the string argument to the getXMLFeed()
function in the following:

var myHTML = "<TR onMouseOver='return overlib(\"<A
HREF=\'javascript:void(0)\'
onClick=\'javascript:getXMLFeed(\"price\")\'>Price </A>\"'>";

How do you do a 5th level nested quote?

Level 1: "
Level 2: '
Level 3: \"
Level 4: \'
Level 5: ?!?!?

Thanks,
Dustin

Randy Webb
Guest
 
Posts: n/a
#2: Aug 13 '06

re: Nested quotation marks/single quotes problem


Dustin said the following on 8/13/2006 11:19 AM:
Quote:
I have a nested quotes issue I'm trying to resolve. Basically I need a 5th
level nested quote for quoting the string argument to the getXMLFeed()
function in the following:
>
var myHTML = "<TR onMouseOver='return overlib(\"<A
HREF=\'javascript:void(0)\'
onClick=\'javascript:getXMLFeed(\"price\")\'>Price </A>\"'>";
>
How do you do a 5th level nested quote?
>
Level 1: "
Level 2: '
Level 3: \"
Level 4: \'
Level 5: ?!?!?
You can double escape it, or, have one of your functions do a replace on
the string and use your own delimiter. But the above code looks like a
horrendous way to do something.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Michael Winter
Guest
 
Posts: n/a
#3: Aug 13 '06

re: Nested quotation marks/single quotes problem


On 13/08/2006 16:19, Dustin wrote:

[snip]
Quote:
var myHTML = "<TR onMouseOver='return overlib(\"<A
HREF=\'javascript:void(0)\'
onClick=\'javascript:getXMLFeed(\"price\")\'>Price </A>\"'>";
>
How do you do a 5th level nested quote?
Include an escaped backslash: \\\"

The important thing to note here, however, is that you've neglected to
remember that an attribute value cannot contain a literal quotation mark
of the same type to delimit the value itself. That is, instead of writing,

""Thanks", he replied." or "\"Thanks\", he replied."

one must write:

"&quot;Thanks&quot;, he replied."

Likewise if using apostrophes, replacing &quot; with ' :

'That's all, folks!'

HTML neither knows nor cares about backslashes as escape delimiters;
only entities are acceptable.

The end result is string like (manually wrapped and concatenated):

"<tr onmouseover='return overlib(\"<a href='#'"
+ " onclick='getXMLFeed(\\\"price\\\");return"
+ " false;'>Price</a>\");'>"

Or, as I prefer to use double quotes as attribute value delimiters:

'<tr onmouseover="return overlib(\'<a href=&quot;#&quot;'
+ ' onclick=&quot;getXMLFeed(\\\'price\\\');return'
+ ' false;&quot;>Price</a>\');">'

Note that I've removed the use of the javascript pseudo-scheme. Read the
archives for why its use in links is a bad idea.

It might help you in future if you work backwards, starting from (in
this case) the markup you'd really want to pass to the overlib function,
working your way "out" to the markup that would contain that. That said,
it would be easier to move all of that out of the event listener into a
separate function.

Mike
Closed Thread


Similar JavaScript / Ajax / DHTML bytes