JavaScript String Script Tag Interference
When embedding a script tag within a quoted JavaScript string, an unexpected issue arises: the resulting script is prematurely terminated. This problem is exemplified in the example provided in the query.
Understanding the Issue
The browser's HTML parser misinterprets the closing script tag () within the string as the end of the actual script element, effectively truncating the script.
Solution
To prevent this misinterpretation, it is advisable to employ the concatenation operator ( ). By splitting the closing script tag into multiple parts, the browser can correctly process the string and prevent premature script termination.
For instance:
<code class="javascript">var test = '...... </scr' + 'ipt>......';</code>
The above is the detailed content of Can HTML Parser Misinterpret Closing Script Tag Embedded in JavaScript String?. For more information, please follow other related articles on the PHP Chinese website!