Why do I get an error message when I print console.log ('< / script >') in my code? It is no problem to type in the console.

1, why is there an error in printing console.log ("< / script >") in the code, while typing it directly in the console will not report an error?
2, print the console.log ("< / script >") code in the code, and the console output is normal?
ask for advice.

Mar.28,2021

Please take a look at this screenshot:

clipboard.png


:

  • JSscript
  • <script></script>

: console.log('\<\/script>')




you may understand when you see the rendering.


because this is the code in the html file.
your html file is first parsed by the browser's html parsing engine. You know, the file itself is just a sequence of strings, and the dom structure will come out only after parsing.

if you give a string '< script > console.log ('< / script >') < / script >', it is parsed to < script > console.log (the tag'< / script > and ') < / script > .

the previous one is the script element, so the browser will pass the content between the tags to the js engine for parsing and execution, obviously, the content is a syntax error.

Menu