What does this mean: ".append ('scr' + 'ipt .."?

4

Yesterday this question was asked: Do something after an external script is fully loaded via jQuery append . Looking at the question and your answer, the following snippet caught my attention:

$("body").append("<scr"+"ipt src='url_do_script.js'></scr"+"ipt>");

What is the meaning and importance of this concatenation?

You could not just do it straight:

$("body").append("<script src='url_do_script.js'></script>");
    
asked by anonymous 06.01.2018 / 23:22

1 answer

5

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.

    
07.01.2018 / 01:02