简体   繁体   中英

echo javascript with single quotes

i got this thing

<?
if (mysql_num_rows($say) == 1) {
    $a = "cicişsin!"; 
}
elseif (mysql_num_rows($say) == 0) {
    $a = "<a href='javascript:LaunchPopup('a2.php','250','1');'>ciciş yap</a>";
}
?>

but i cant echo second $a.. its exits at "javascript:LaunchPopup(" single quotes not shown

what can i do?

$a = "<a href=\"javascript:LaunchPopup('a2.php','250','1');\">ciciş yap</a>";

Never use javascript: URLs. Put the URL in the href attribute where it belongs:

$a= '<a href="a2.php" onclick="LaunchPopup(this.href, 250, 1); return false;">ciciş yap</a>';

Now not only do you not have to worry about the escaping (assuming you can get away with passing numbers as the other arguments), but also your link now works properly when middle-clicked or bookmarked, instead of giving a JavaScript error.

Better still, unobtrusive scripting:

<a href="a2.php" class="popup">ciciş yap</a>

<script type="text/javascript">
    for (var i= document.links.length; i-->0;) {
        if (document.links[i].className==='popup') {
            document.links[i].onclick= function() {
                LaunchPopup(this.href, '250', '1');
                return false;
            }
        }
    }
</a>

Keep script code in scripts and out of markup, then you don't have to worry about HTML-escaping.

逃避这样的报价

"<a href=\"javascript:LaunchPopup(\'a2.php\',\'250\',\'1\');\">ciciş yap</a>"

使用反斜杠,如下所示:

  $a = "<a href='javascript:LaunchPopup(\"a2.php\",\"250\",\"1\");'>ciciş yap</a>";
$a = "<a href='javascript:LaunchPopup(a2.php,250,1)'>ciciş yap</a>";

会为你工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM