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 &
.
<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>
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;
精彩评论