Prototype This page is part of the Prototype JavaScript framework documentation.
Overview | (...)

String.prototype.unescapeHTML takes a string, removes tags and comments, decodes HTML entities, and returns a copy of the decoded string. That is to say that it returns a copy of the string with HTML entities (’&lt;’, ’&gt;’, ’&amp;’, ’&quot;’, ’&copy;’, etc…) replaced with corresponding characters (’<’, ’>’, ’&’, ’"’, ’©’, etc…).

Syntax


  string.unescapeHTML();

Returns

A copy of string with HTML entities replaced with corresponding characters.

Examples


<script type="text/javascript">
   var stringEscapedHTML = '&lt;p&gt;foo &amp;amp; bar&lt;/p&gt;'
   var stringUnescapedHTML = stringEscapedHTML.unescapeHTML();
      // stringUnescapedHTML = '<p>foo &amp; bar</p>';

   var stringUnescapedHTML = '&lt;p&gt;foo &amp;amp; bar&lt;/p&gt;'.unescapeHTML();
      // stringUnescapedHTML = '<p>foo &amp; bar</p>';

   var stringDoubleUnescapedHTML = stringUnescapedHTML.unescapeHTML();
      // stringDoubleUnescapedHTML = 'foo & bar';
</script>

stringEscapedHTML remains equal to ’&lt;p&gt;foo &amp;amp; bar&lt;/p&gt;’.

stringUnescapedHTML is equal to ’<p>foo &amp; bar</p>’.

stringDoubleUnescapedHTML is equal to ‘foo & bar’. While some might have expected it to be equal to ’<p>foo & bar</p>’, it isn’t. This is because the String.prototype.stripTags is called before decoding the string.


<script type="text/javascript">
   var stringUnescapedHTML = '&quot;Lions, tigers, bears, etc&#8230;&quot;'.unescapeHTML();
      // var stringUnescapedHTML = '"Lions, tigers, bears, etc…"';
</script>

stringUnescapedHTML is equal to ’"Lions, tigers, bears, etc…"’. While String.prototype.escapeHTML only encodes special characters to HTML entities, String.prototype.unescapeHTML decodes more than those special HTML entities.


<script type="text/javascript">
   var stringHTML = '<p>&quot;Lions, tigers, bears, etc&#8230;&quot;</p>'
   var equal = (stringHTML == stringHTML.escapeHTML().unescapeHTML());    
     // equal = true;
</script>

stringHTML is equal to stringHTML.escapeHTML().unescapeHTML(). While String.prototype.unescapeHTML decodes more HTML entities to characters than String.prototype.escapeHTML encodes special characters to HTML entities, calling the String.prototype.unescapeHTML method on a string which the String.prototype.escapeHTML method has been called will return the original string.

Notes

String.prototype.unescapeHTML uses the proprietary (not official DOM) but widely supported innerHTML property.

String.prototype.unescapeHTML calls the String.prototype.stripTags method. Any tags or comments will be removed before the string is decoded. Also, since String.prototype.stripTags does not actually validate the HTML, partial and/or broken tags may result in the removal of more of the string than expected.