JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.11

Using HTML Entities to Protect HTML Tags

This section provides a tutorial example on how to use HTML entities < and > to protext HTML tags

To completely resolve the problem we saw in the previous section, we need to use HTML entities to replace '<' and '>', if you want some HTML tags to be displayed as page content.

Here is the revised HTML document using &lt; and &gt; entities:

<html>
<!-- Escape_Script_Tag_HTML_Entity.html
   Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head><title>Escaping Script Tags</title></head>
<body><pre>
<script type="text/javascript">
   document.writeln("Q: How to use the &lt;script&gt; tag?");
   document.writeln("A: &lt;script&gt;...Code...&lt;\/script&gt;");
   document.writeln("Rate this answer: 1 | 2 | 3 | 4 | 5");
</script>
</pre></body>
</html>

If you run this example script in a browser, you will get:

Q: How to use the <script> tag?
A: <script>...Code...</script>
Rate this answer: 1 | 2 | 3 | 4 | 5

Finally, we get what we want!

Sections in This Chapter

JavaScript Support in Web Browsers

Including JavaScript Codes with HTML "script" Tags

Including 'script' Tags in String Literials

Escaping 'script' Tags in String Literials

Using HTML Entities to Protect HTML Tags

Including JavaScript Codes as External Files

DOM API - The "document" Object

DOM API - The "window" Object

Event Listeners and Objects

'javascript:' Pseudo-URL Addresses

Dr. Herong Yang, updated in 2008
Using HTML Entities to Protect HTML Tags