¿Por qué el navegador no puede analizar el siguiente código?

<script type='text/javascript'>

    function cool()
    {
        var innerHtml = "<script></script>";//simply for demostration
        $("body").append(innerHtml);
    }
 </script>

The above will confuse browsers and will not render correctly before the cool function is even invoked. Why?

preguntado el 08 de noviembre de 11 a las 18:11

Because browsers will choke on the </script> inside the JavaScript string, The HTML parser does not understand JavaScript, and will only see this token and will close the script block. -

It's quite confusing as to what you're doing here. Can you please explain your problem more clearly, with more detail? -

Quieres el <script> tags to appear as text or execute? -

@JamWaffles What other details are needed? -

1 Respuestas

</script> will end a script element, so your script terminates in the middle of a string literal.

Browsers are not "confused", the HTML is simply wrong (and invalid … except in XHTML … but that only counts if you are using application/xhtml+xml … and then you get a different set of issues), which leads to the JavaScript being wrong.

Represent the sequence </ as <\/ in JS strings when you are using inline JS (as per the example in section 18.2.4 of the HTML 4.01 Recommendation).

respondido 08 nov., 11:22

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.