This is used when the script is placed directly inside the HTML. For example, doing 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 in fact it ended in
...);
</script>
See the test:
<script>
document.write("<script src='url_do_script.js'></script>");
</script>
Then, when it splits into two strings and concatenates, it will not interfere with the rendering engine.
However, it is important to note that this is not required at opening tag:
"<scr"+"ipt
Because the script will only fail to render and execute if it is in the closing tag. Another interesting thing is that instead of using 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 it inside a .js
then it is a mistake of the programmer, within .js
you can write </script>
quietly in strings that will not affect anything.
You can also make an escape on the bar
barra
of</script>
, thus:$("body").append("<script src='url_do_script.js'><\/script>");
– Sam
I must be 15 years old
<scr"+"ipt
, when I learned, and I never bothered to recycle... I never noticed that I only had to do it in the<\script>
as quoted by William. But now I will use the form above, with escape. I found it much simpler.– Sam
Until then I had never seen this rsrs.
– NoobSaibot
Years ago I worked in a web advertising company and the agencies sent me scripts like this to put on the site. :)
– Sam