Arno Kuhl <[email protected]> wrote:
>I've been battling with quotes encoding when outputting javascript with
>php.
>It can't be unique, so I'm hoping someone has a working solution
>they're
>willing to share.
>
>The following works perfectly as long as there aren't any single quotes
>in
>the link text:
> echo "<span onclick=\"insertLink('$sUrl','$sTitle')\"
>class='linkSel'>$sTitle</span>";
>
>if $sTitle has the value What's new it outputs:
> <span onclick="insertLink('article/whats-new.html','What's
>new')" class='linkSel'>What's new</span>
>
>It displays fine, but javascript complains with:
> Expected ')' linkmanager.php Line:525 Char:63
>
>
>So I fix this by swapping the double and single quotes around:
> echo "<span onclick='insertLink(\"$sUrl\",\"$sTitle\")'
>class='linkSel'>$sTitle</span>";
>
>Now for that specific link it outputs:
> <span onclick='insertLink("article/whats-new.html","What's
>new")' class='linkSel'>What's new</span>
>And javascript is happy.
>
>But elsewhere there's a link Fred "Buster" Cox and it outputs:
> <span onclick='insertLink("article/fred-buster-cox.html","Fred
>"Buster" Cox")' class='linkSel'>Fred "Buster"
>Cox</span>
>
>Again it displays fine, but javascript complains with:
> Expected ')' linkmanager.php Line:743 Char:77
>
>
>So it looks like I can't have links that include single quotes and
>double
>quotes, only one or the other.
>
>One work-around I thought of was to convert any link texts that
>included
>double quotes into single quotes when the content is posted, and it
>would
>then be displayed with single quotes even though the user entered
>double
>quotes. It's far from ideal but it would work, though I can think of a
>few
>situations where it would be quite confusing to the reader. Are there
>any
>other solutions that would allow both types of quotes without any
>conversions?
>
>Cheers
>Arno
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
You aren't escaping the quotes correctly when they go into your output. You're
escaping them for html not javascript. Javascript (like php) escapes single
quotes inside a single quote string with a back slash.
Thanks,
Ash
http://ashleysheridan.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php