我目前正在使用PHP动态创建一个javascript,它将是页面上的echoed。
示例代码:
$JS .= '<script>';
if($condition == true) {
$JS .= 'alert("Yo its true omg!");
}
$JS .= "</script>";正如您所看到的,这最终会使所有'引号和双引号中的单引号转义变得混乱.
有更好的方法吗?
发布于 2017-11-14 08:48:56
如果静态javscript代码的数量超过了您以二进制方式创建的javascript代码量,那么您可以像往常一样编写javascript代码,然后在需要时注入php。
假设的index.php示例:
<?php
$message = "Yo its true omg!";
?>
<html><head>
<script>
alert("<?php echo $message; ?>");
</script>
</head><body>
...
</body></html>https://stackoverflow.com/questions/6584438
复制相似问题