简体   繁体   English

用单引号回显javascript

[英]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 但我不能回应第二个$ a ..它的出口在“javascript:LaunchPopup(”单引号未显示

what can i do? 我能做什么?

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

Never use javascript: URLs. 永远不要使用javascript: URL。 Put the URL in the href attribute where it belongs: 将URL放在它所属的href属性中:

$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. 现在,您不仅不必担心转义(假设您可以通过其他参数传递数字),而且您的链接现在可以在中间点击或加入书签时正常工作,而不是给出JavaScript错误。

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. 将脚本代码保留在脚本中并且不在标记之外,那么您不必担心HTML转义。

逃避这样的报价

"<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>";

会为你工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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