开发者

Problems with javascript inside echo

I want to echo the following code:

<a href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;" class="delicious" target="_blank">Add to Delicious</a>

The problem is that Dreamweaver warns me on syntax errors I can not figure out. Who can spot the syntax errors? The html and sc开发者_JAVA百科ript on its own works just fine - the problem comes with the php when it is echoed.


you can use:

<?php
echo '<a href="http://delicious.com/save" onclick="window.open(\'http://delicious.com/save?v=5&noui&jump=close&url=\'+encodeURIComponent(location.href)+\'&title=\'+encodeURIComponent(document.title), \'delicious\',\'toolbar=no,width=550,height=550\'); return false;" class="delicious" target="_blank">Add to Delicious</a>';
?>

not tested, but should work.


You shouldn't have & in HTML, these should be encoded to &amp;.

<a href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;" class="delicious" target="_blank">Add to Delicious</a>

That's the only error I can spot.


You have to escape " chars :

echo "<a href=\"http://delicious.com/save\" onclick=\"window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;\" class=\"delicious\" target=\"_blank\">Add to Delicious</a>";

As bluesmoon said it's better written as :

echo <<<ENDOFECHO
<a href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;" class="delicious" target="_blank">Add to Delicious</a>";
ENDOFECHO;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜