This is used when the script is placed directly inside the HTML. For example, if you do this:
<script>
$("body").append("<script src='url_do_script.js'></script>");
</script>
The browser's HTML rendering engine will think that the script has ended in
...</script>");
When it actually ended in
...);
</script>
See the test:
<script>
document.write("<script src='url_do_script.js'></script>");
</script>
So when it splits into two strings and concatenates, it will not disturb the rendering engine.
However, it is important to note that this is not required in the opening tag:
"<scr"+"ipt
Because the script will only fail rendering and execution if it is in the closing tag. Another interesting thing is that instead of using the concatenate </scr" + "ipt>
, you can simply use the HTML comments, like this:
<script>
<!--
$("body").append("<script src='url_do_script.js'></script>");
-->
</script>
See the test:
<script>
<!--
document.write("<script src='url_do_script.js'></script>");
-->
</script>
If you noticed this within .js
then it is a programmer error, within .js
you can write </script>
quietly in strings that will not affect anything.