{"id":4348,"date":"2025-09-23T10:18:00","date_gmt":"2025-09-23T09:18:00","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=4348"},"modified":"2025-09-25T11:35:41","modified_gmt":"2025-09-25T10:35:41","slug":"newline","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/text\/newline","title":{"rendered":"Newline"},"content":{"rendered":"\n<p>Whilst &#8220;\\n&#8221; is the normal way of adding a newline to a JavaScript string (or &#8220;\\r\\n&#8221;), if you do it inside a php heredoc string, it gets stripped and turned into a newline in the output, so its lost from the JavaScript string and the browser see&#8217;s the string continue on a new line causing a JavaScript error.<\/p>\n\n\n\n<p>You can use \\\\n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $HtmlOutput .= &lt;&lt;&lt;_END\n    &lt;script>\n      const text = \"The first line of my string:\\\\nThe second line of my string\";\n    &lt;\/script>\n_END;<\/code><\/pre>\n\n\n\n<p>You can also use fromCharCode to solve this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $HtmlOutput .= &lt;&lt;&lt;_END\n    &lt;script&gt;\n      const text = \"The first line of my string:\" + String.fromCharCode(10) + \"The second line of my string\";\n    &lt;\/script&gt;\n_END;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Whilst &#8220;\\n&#8221; is the normal way of adding a newline to a JavaScript string (or &#8220;\\r\\n&#8221;), if you do it inside a php heredoc string, it gets stripped and turned into a newline in the output, so its lost from the JavaScript string and the browser see&#8217;s the string continue on a new line causing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[],"class_list":["post-4348","post","type-post","status-publish","format-standard","hentry","category-text"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/comments?post=4348"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4348\/revisions"}],"predecessor-version":[{"id":4356,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4348\/revisions\/4356"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=4348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=4348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=4348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}