', ", \", then what?

A

Alias

dynamically creating a number of thumbnails, which is working fine. However,
they are supposed to call a function loadRightFrame when clicked, and I seem
to be running into syntax problems. This code:


pagecontent+='<img width="70" height="70" border="0" src="' + thumbArray
+ '" onClick="loadRightFrame(\"' + photoArray + '\")">'

resolves to this:

pagecontent+='<img width="70" height="70" border="0"
src="pictures/unt/kl1.jpg" onClick="loadRightFrame("pictures/unt/gr1.jpg")">

I obviously cannot use double quotes OR single quotes when passing
parameters to the loadRightFrame function. Escaping the quotes is not
working either. Any ideas?


Greets,

A
 
L

Lasse Reichstein Nielsen

Alias said:
pagecontent+='<img width="70" height="70" border="0" src="' + thumbArray
+ '" onClick="loadRightFrame(\"' + photoArray + '\")">'

resolves to this:

pagecontent+='<img width="70" height="70" border="0"
src="pictures/unt/kl1.jpg" onClick="loadRightFrame("pictures/unt/gr1.jpg")">


Try:
pagecontent+='<img width="70" height="70" border="0" src="' + thumbArray
+ '" onClick="loadRightFrame(\'' + photoArray + '\')">'

So it's: ', ", \', \\\", \\\\\', ...

Try:
---
var foo = 42;
var s = '"\'\\\"\\\\\'foo\\\\\'\\\"\'"';
alert(s);
while (typeof s == "string") {
s = eval(s);
alert(s);
}
---
I obviously cannot use double quotes OR single quotes when passing
parameters to the loadRightFrame function. Escaping the quotes is not
working either. Any ideas?

Escape single quotes.

When in doubt about something like this, I prefer to move backwards.
Start with the desired result, and then add quotes around it add
appropriate escapes.

Start with valid HTML (except the dynamic parts, which we mark so
we can remember where they are):
---
<img width="70" height="70" border="0"
src="{thumbArray}" onClick="loadRightFrame('{photoArray}')">
---
Then put it in quotes of some sort. Since the string contains fewest
single quotes, that's what we'll use. That means that existing
single quotes must be escaped (if put double quotes around it, then
we would have to escape all double quotes). If there were any
backslashes, they would have to be escaped as well.
---
'<img width="70" height="70" border="0"' +
' src="{thumbArray}" onClick="loadRightFrame(\'{photoArray}\')">'
 
A

Alias

Lasse Reichstein Nielsen wrote:
||
||| Try:
||| ---
||| var foo = 42;
||| var s = '"\'\\\"\\\\\'foo\\\\\'\\\"\'"';
||
|| Actually, following the method I gave below, this would become:
|| ---
|| var s = '"\'\\"\\\\\'foo\\\\\'\\"\'"';
|| ---
|| which is two needless backslashes shorter.
||
|| /L
|| --
|| Lasse Reichstein Nielsen - (e-mail address removed)
|| DHTML Death Colors:
|| <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith
|| without judgement merely degrades the spirit divine.'

Thanks Lasse, working now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top