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

Asked

Viewed 229 times

4

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

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

What is the meaning and importance of this concatenation ?

Couldn’t just do it straight:

$("body").append("<script src='url_do_script.js'></script>");
  • You can also make an escape on the bar barra of </script>, thus: $("body").append("<script src='url_do_script.js'><\/script>");

  • 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.

  • Until then I had never seen this rsrs.

  • Years ago I worked in a web advertising company and the agencies sent me scripts like this to put on the site. :)

1 answer

4


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.