SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with double quotes in Javascript link

    I have the following PHP which passes a variable to two other frames:
    PHP Code:
    $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.'/');">&nbsp;'.$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).
    Why doesn't the link work with double quotes in the variable?
    Thanks, Jack.

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try viewing the actualy html that arrives at the browser. Should be pretty clear what has gone wrong from that.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The HTML has the values for name with backslashes in front of single quotes and double quotes. The link works OK for names with a single quote, but when I hover over a name with a double quote the url string shown in the status bar at the bottom of the browser stops at the double quote (it shows the backslash but not the double quote). It seems that the backslash is not working as a double quote escape for the Javascript - it thinks it is the end of the string.

  4. #4
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post the browser HTML, I know your description is clear to you, but my poor head can't make sense of it. I've had this problem before and should be able to help if I see your HTML.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry this is so long, but my original post was just a simplification. I thought I better post 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>

  6. #6
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the problem lies in the fact that you want to pass the double quotes as a URL argument to the next php file.

    I'm not sure what the function is in PHP, but in ASP all parameters passed via the URL must be URL encoded with Server.URLEncode() which turns spaces into those cool strings (%2D).

    Assuming you're not familiar with the you have to be careful to only encode the argument names and values, i.e. don't pass the entire string in because it will convert your '&' and '?' into an encoded version.

    hth.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried urlencode() (PHP) which converts characters as you suggest, but that doesn't work. I get %22 for " and %27 for ' in the HTML. However, the browser status bar just shows the ' and " when I hover (not escaped), and doing it this way neither the single nor double quote examples work.
    I read somewhere that Javascript doesn't like the %.. characters.

  8. #8
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you have to url encode them, so what I suggest is that you put it back in and then post the HTML that is generated from that. (You are correct the link will show the single/double quote in the status bar - thats what is supposed to happen.) However the link should also work. JavaScript can handle any characters so don't worry about that.

    Put the encoding back in, post the browser HTML and we'll take it from there.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  9. #9
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dale, thanks very much for your continuing help. I have used PHP rawurlencode() which also replaces spaces with %20. Here is the HTML:

    <tr><td><a href="javascript:
    void(parent.objecttitle.location='objecttitle.php?son=Alfred%20Scott&con=%20&src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=Alfred%20Scott&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=22&coi= &con=%20');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%20Scott&coi= &con=%20');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%20Scott&coi= &con=%20');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%20Scott&coi= &con=%20');void(parent.options.location='options.php?src=OL&s=T&soi=22&son=Alfred%20Scott&sti=4');">&nbsp;Alfred Scott</a></td></tr>
    <tr><td><a href="javascript:
    void(parent.objecttitle.location='objecttitle.php?son=Carl%20Fogarty&con=%20&src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=Carl%20Fogarty&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=84&coi= &con=%20');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%20Fogarty&coi= &con=%20');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%20Fogarty&coi= &con=%20');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%20Fogarty&coi= &con=%20');void(parent.options.location='options.php?src=OL&s=T&soi=84&son=Carl%20Fogarty&sti=4');">&nbsp;Carl Fogarty</a></td></tr>
    <tr><td><a href="javascript:
    void(parent.objecttitle.location='objecttitle.php?son=double%22quotename&con=%20&src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=double%22quotename&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=86&coi= &con=%20');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%22quotename&coi= &con=%20');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%22quotename&coi= &con=%20');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%22quotename&coi= &con=%20');void(parent.options.location='options.php?src=OL&s=T&soi=86&son=double%22quotename&sti=4');">&nbsp;double"quotename</a></td></tr>
    <tr><td><a href="javascript:
    void(parent.objecttitle.location='objecttitle.php?son=single%27quotename&con=%20&src=OL&s=T&stae=N');void(parent.objectdetails.location='objectdetails.php?src=OL&s=T&son=single%27quotename&sti=4&stn=People&stoh=a person&stch=more info&stf=A&stae=N&socm=N&soi=85&coi= &con=%20');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%27quotename&coi= &con=%20');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%27quotename&coi= &con=%20');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%27quotename&coi= &con=%20');void(parent.options.location='options.php?src=OL&s=T&soi=85&son=single%27quotename&sti=4');">&nbsp;single'quotename</a></td></tr>

  10. #10
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks fine to me - I get a JavaScript error when I run it - Invalid Object? So maybe you have a few other things to sort out - but the link looks OK.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  11. #11
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Update: using rawurlencode() the link now works for the double quote example but not the single quote example!
    I think this gives me something to go on - thanks for your help.
    Jack.

  12. #12
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me know how it goes.

    BTW: what does the JavaScript void() do? I always thought that JavaScript statements could stand alone?
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  13. #13
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At last some progress - using addslashes() and then urlencode() to the result has done the trick - the link works.
    However, it has created chaos in the linked to frames, but I think I will be able to sort that out.
    I don't know much about Javascript (I have been mainly copying stuff), but if I remember correctly the 'void' stops the javascript returning a result which otherwise would be included in the HTML and display in the browser - something like that anyway.
    Thanks for your help, Jack.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •