Can't add new lines in JavaScript alert Box?

前端 未结 2 1406
自闭症患者
自闭症患者 2021-01-24 10:45

I\'m generating a string in PHP and then eventually passing this string into a JavaScript alert box, my problem is I actually can\'t add line breaks in my alert box.

My c

相关标签:
2条回答
  • 2021-01-24 11:20

    You need to change $str to

    $str = "This is a string\\n";
    

    so that the \n gets passed to the JavaScript.

    0 讨论(0)
  • 2021-01-24 11:25

    This happens because PHP interprets the \n before JavaScript has the chance to, resulting in a real line break inside the Javascript code. Try

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