View Full Version : Problem with double quotes in Javascript link
Jack101
02-10-2003, 09:11 AM
I have the following PHP which passes a variable to two other frames using a Javascript link:
PHP:
$name=addslahes($row['name']);
echo('<tr><td><a href="javascript:
void(parent.frameB.location=/'frameB.php?name='.$name.'&desc='.$desc.'/');
void(parent.frameC.location=/'frameC.php?name='.$name.'&number='.$number.'/');"> '.$row['name'].'</a></td></tr>');
(Note that I have put / in above where there is really a \ just so that it shows up in the post).
The link works fine with normal characters in the variable $name, and it works with single quotes in the varable, but not double quotes (the addslashes() escapes single and double quotes with a backslash).
In the browser status bar the url string shown when I hover over the link stops at the double quote - whereas with a single quote it shows the whole url. Why doesn't the link work with double quotes - does the Javascript think that the double quote is the end of the string, and if so how do I escape the double quote for Javascript?
Thanks in anticipation, Jack.
Roelf
02-10-2003, 09:21 AM
Originally posted by Jack101
does the Javascript think that the double quote is the end of the string, and if so how do I escape the double quote for Javascript?
yes, Javascript "thinks" it's the end of the string
escape it with a backslash
Jack101
02-10-2003, 09:36 AM
Thanks Roelf, but the PHP addslashes() function is adding backslashes to single and double quotes - I can see that the backslashes are there in the generated HTML.
Roelf
02-10-2003, 09:53 AM
then post the generated string here, that way we can look where it goes wrong
Jack101
02-10-2003, 10:53 AM
Sorry this is so long, but my original post was just a simplification. I thought I better post it the whole lot in case my error is somewhere else.
You will see I have four repetitions in this example - there are four 'names' (son): Alfred Scott, Carl Fogarty, single'quotename and double"quotename. The single and double quotes in the last to are both escaped with a backslash. The link works for all but double"quotename.
<tr><td><a href="javascript:
void(parent.objecttitle.location='objecttitle.php?son=Alfred Scott&con= &src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=Alfred Scott&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=22&coi= &con= ');void(parent.accesshistory.location='accesshistory.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=22&son=Alfred Scott&coi= &con= ');void(parent.contentlistheading.location='contentlistheading.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=22&son=Alfred Scott&coi= &con= ');void(parent.contentlist.location='contentlist.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=22&son=Alfred Scott&coi= &con= ');void(parent.options.location='options.php?src=OL&s=T&soi=22&son=Alfred Scott&sti=4');"> Alfred Scott</a></td></tr>
<tr><td><a href="javascript:
void(parent.objecttitle.location='objecttitle.php?son=Carl Fogarty&con= &src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=Carl Fogarty&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=84&coi= &con= ');void(parent.accesshistory.location='accesshistory.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=84&son=Carl Fogarty&coi= &con= ');void(parent.contentlistheading.location='contentlistheading.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=84&son=Carl Fogarty&coi= &con= ');void(parent.contentlist.location='contentlist.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=84&son=Carl Fogarty&coi= &con= ');void(parent.options.location='options.php?src=OL&s=T&soi=84&son=Carl Fogarty&sti=4');"> Carl Fogarty</a></td></tr>
<tr><td><a href="javascript:
void(parent.objecttitle.location='objecttitle.php?son=double\"quotename&con= &src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=double\"quotename&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=86&coi= &con= ');void(parent.accesshistory.location='accesshistory.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=86&son=double\"quotename&coi= &con= ');void(parent.contentlistheading.location='contentlistheading.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=86&son=double\"quotename&coi= &con= ');void(parent.contentlist.location='contentlist.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=86&son=double\"quotename&coi= &con= ');void(parent.options.location='options.php?src=OL&s=T&soi=86&son=double\"quotename&sti=4');"> double"quotename</a></td></tr>
<tr><td><a href="javascript:
void(parent.objecttitle.location='objecttitle.php?son=single\'quotename&con= &src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=single\'quotename&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=85&coi= &con= ');void(parent.accesshistory.location='accesshistory.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=85&son=single\'quotename&coi= &con= ');void(parent.contentlistheading.location='contentlistheading.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=85&son=single\'quotename&coi= &con= ');void(parent.contentlist.location='contentlist.php?src=OL&s=T&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=85&son=single\'quotename&coi= &con= ');void(parent.options.location='options.php?src=OL&s=T&soi=85&son=single\'quotename&sti=4');"> single'quotename</a></td></tr>
ca_redwards
02-10-2003, 11:02 AM
q=String.fromCharCode(34);
Then when building a javascript: link, just use q wherever you need a double quote.
javascript:document.write(q+'double quoted'+q)
I use this technique in my HTML() bookmarklet library (http://www.angelfire.com/ca/redwards/html__.calendar.html) to ensure that the entire javascript link is double-quote-free.
Jack101
02-10-2003, 11:25 AM
I'm afraid you'll have to help me a bit - I hardly know Javascript at all.
Where would this code go in my PHP program?
If I were to substitute a double quotes for a 'q' in my variable, then everytime there is a real 'q' won't it be converted back to double quotes somewhere?
Why doesn't the simple backslash escape work?
Thanks, but I am very confused.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.