JavaScript and slashes in strings

What is the real reason that we should avoid the slash in the JavaScript string, and also why we should avoid the string / no string in XHTML. Many tutorials will simply reveal this problem.

+7
source share
3 answers

What is the real reason we need to escape the slash in a JavaScript string

In an HTML 4 document, the </ sequence within an element defined as containing CDATA (for example, a script) is the end tag and terminates the element (with an error if it is not </script> .

In relation to JS / and \/ identical inside the string. As for HTML, </ runs the end tag, but <\/ does not.

and also why should we avoid string / no strings in XHTML.

XHTML does not provide a method for indicating that an element contains embedded CDATA, so you need to explicitly handle characters that would otherwise have special meaning ( < , & , etc.). Their wrapper contains an element with CDATA markers - this is the easiest way to achieve this.

+7
source

You do not need to hide / in the JavaScript line, just \ , because if you do not, the line yes\no will be inadvertently converted to yes<newline>o . Exiting \ prevent this.

In addition, if you do not escape & in the URL, then everything that comes after it will be considered a new parameter. For example, a=Q&A will mean "parameter a has the value" Q ", and also parameter a " instead of "parameter a has the value" Q&A "". The correct way to avoid this would be a=Q%26A .

+3
source

To prevent browsers, especially older ones, a slash is required to erroneously interpret the forward slash as a closing JavaScript marker.

0
source

All Articles