Expanding on someone else's answer:
(扩展别人的答案:)
<script>
var myvar = <?php echo json_encode($myVarValue); ?>;
</script>
Using json_encode() requires:
(使用json_encode()需要:)
Since UTF-8 supports full Unicode, it should be safe to convert on the fly.
(由于UTF-8支持完整的Unicode,因此动态转换应该是安全的。)
Note that because json_encode
escapes forward slashes, even a string that contains </script>
will be escaped safely for printing with a script block.
(请注意,因为json_encode
转义正斜杠,所以即使是包含</script>
的字符串也会安全地转义,以便使用脚本块进行打印。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…