Previously, I am encountering "unterminated string literal strange error" in JS: SyntaxError: unterminated string literal strange error
I kinda fix it using json_encode since newlines are escaped.
Now I am getting these odd results, for example this JS variable (processed with json_encode):
cacheObj_open.handler='<pre class="brush: html;">"<html>\r\n<body>\r\n<p>Hello world.<\/p>\r\n<\/body>\r\n<\/html>"</pre>';
Will output with codes having double quotes around them:
"<html>
<body>
<p>Hello world.</p>
</body>
</html>"
The above code runs without console error. But the one below (also processed with json_encode) throws SyntaxError: missing ; before statement error:
cacheObj_open.handler='<pre class="brush: html;">"\r\n<?php\r\n$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");\r\nif ($stmt->execute(array($_GET['name']))) {\r\n while ($row = $stmt->fetch()) {\r\n print_r($row);\r\n }\r\n}\r\n?>"</pre>';
What is the best way to avoid any errors when outputting HTML entity source code to a JS variable? I know that json_encode will escape some new lines(this is a good idea) but based on the above example, it still throws an error for some codes. I appreciate any inputs and suggestions.
I have tried adding addslashes on PHP side but it still throws the error.