The Hidden Trap of Unescaped Newlines in PHP Strings
Encountering the browser console error '"' string literal contains an unescaped line break is a classic symptom of mixing server-side rendering logic with client-side JavaScript expectations. This specific error usually surfaces when you attempt to inject raw PHP output directly into a JavaScript string without properly escaping special characters, particularly newline sequences.
The core issue in your scenario stems from how the browser's JavaScript engine parses the code within the <script> tag versus how PHP renders the content. When you execute echo $faq['content']; directly in a view file, the output is treated as raw HTML or text. However, when you embed that same output inside a JavaScript string literal using .html(), the browser first needs to parse your JavaScript code before it ever sees the rendered PHP content.
If $faq['content'] contains a newline character (e.g., \n) or an actual line break in the source file, the resulting JavaScript looks like this to the browser parser:
".html('<div>...content with\nnewlines...</div>');"
The JavaScript engine sees the literal characters " followed immediately by a newline. In standard JavaScript syntax, a string cannot span multiple lines unless the newline is escaped (e.g., \n) or enclosed in template literals with specific care. When the parser hits that raw newline inside the double-quoted string, it throws the syntax error: '"' string literal contains an unescaped line break.
Why Direct Echo Fails in Script Tags
The confusion often arises from treating PHP output as if it were already sanitized for JavaScript. The echo statement sends a string to the browser's document object model (DOM). It does not know what you are doing with that string afterward. If you construct your HTML injection using .html(), you are essentially building a JavaScript string dynamically.
In your provided code snippet:
setTimeout(function () {
$(".opencxbody").html('<div style="max-height:400px; overflow-y: scroll;"> <?php echo $faq['content']; ?></div>');
}, 100);
You are attempting to use a JavaScript string literal delimited by single quotes ('). While single quotes allow you to use double quotes inside the string without escaping, they do not protect against newlines within the string content itself.
If $faq['content'] is:
Line one
Line two
The generated JavaScript becomes invalid syntax because of the physical line break inside the quoted string.
The Correct Approach: HTML Escaping
To resolve this, you must ensure that any data coming from PHP destined for a JavaScript string literal is escaped specifically for JavaScript contexts. This means converting newlines into \n sequences and escaping double quotes (") to \".
Laravel provides the e() helper function which can be configured or used in combination with custom encoding, but often the most robust solution is to let PHP handle the text rendering entirely outside of a dynamic JavaScript string. Instead of passing the raw content into a JS string, render the HTML directly on the server side and let jQuery manipulate the DOM element after the page loads.
However, if you must inject via JavaScript, use htmlentities() or similar escaping functions to convert special characters:
<script>
setTimeout(function () {
// Escape newlines as \n and quotes as \" for valid JS syntax
var safeContent = str_replace("\n", "\\n", addslashes($faq['content']));
$(".opencxbody").html('<div style="max-height:400px; overflow-y: scroll;">' + safeContent + '</div>');
}, 100);
</script>
Note that addslashes() adds quotes around the string if it contains a quote, but it also escapes backslashes. For complex content containing both newlines and quotes, a more thorough replacement or using a dedicated library is safer to avoid breaking out of the JavaScript string entirely.
A Better Architectural Pattern
A common pitfall in this situation is mixing server-side rendering logic with client-side manipulation when a simpler approach exists. If the goal is simply to display FAQ content inside a scrollable div, consider moving the PHP code outside the <script> block and appending it directly to the HTML string before passing it to jQuery.
<div style="max-height:400px; overflow-y: scroll;">
<?php echo $faq['content']; ?>
</div>
<script>
$(".opencxbody").html(divContent); // Ensure divContent is defined in PHP above
</script>
By separating the concerns, you avoid the JavaScript parser trying to interpret raw newline characters from your database as part of a string literal. This architectural shift eliminates the need for manual escaping of newlines within the script block, reducing the risk of syntax errors and improving code maintainability. Always prioritize server-side rendering for content that originates from the database unless dynamic client-side logic strictly requires immediate injection.