Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange unterminated string literal error with Firefox [duplicate]

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?

like image 526
Jarym Avatar asked Dec 12 '25 08:12

Jarym


1 Answers

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>
like image 172
Musa Avatar answered Dec 14 '25 22:12

Musa