How to encode a URL as javascript string in PHP-generated HTML?

后端 未结 1 539
滥情空心
滥情空心 2021-01-22 06:53

Given this PHP code:

\'\"

What if there is a \' in $url?

相关标签:
1条回答
  • 2021-01-22 07:52

    json_encode will work. You just have to use it the right way:

    <a onclick="javascript:window.location.href=<?php echo htmlspecialchars(json_encode($url)); ?>">
    

    This will work since json_encode already returns an JavaScript expression with quotes. And htmlspecialchars is needed to escape possible HTML meta characters.

    0 讨论(0)
提交回复
热议问题