I have seen similar issues to the below but I've thoroughly checked the following problem. I receive an "Unterminated string literal" error in Firefox's console.
I have an HTML page as follows:
<HTML>
<HEAD>
<SCRIPT type="text/JavaScript">
var test = "</script>";
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
As far as I can tell, there should be nothing invalid about this JavaScript since I don't believe '<', '/' or '>' to need escaping when used as part of a JavaScript string variable. In fact the following works:
var test = "</>";
So, I am wondering how my little script is managing to trip up the browser or how I can work around it?
Its not the js you have to worry about its the html.
The html parser reads your "</script>" string as an end tag closing the script tag, ending the script. So the js interpreter sees
var test = "
To avoid this you can use an escape trick
<SCRIPT type="text/JavaScript">
var test = "<\/script>";
</SCRIPT>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With