Escaping 'script' Tags in String Literials
Now let's try to fix the problem we found in the previous section.
Here is the revised HTML document with the extra backslash character:
If you run this example script in a browser, you will get:
Question: How to use the Rate this answer: 1 | 2 | 3 | 4 | 5
Better? Yes. But this is still not what we expected. Here is what happened when the browser processes this HTML document:
Question: How to use the <script> tag: Answer: <script>...Script Code...<\/script> Rate this answer: 1 | 2 | 3 | 4 | 5
When the browser renders this content, it will treat "script" tags as HTML tags, causing some text not showing on the final page.
See the next section on how to protect "script" tags in HTML documents.
Table of Contents