View Full Version : Useful Prototype: htmlEntities()

07-25-2008, 03:56 PM
This method escapes all &, <, and > symbols in the string, making it safe to display the string on a web page without fear that it will be treated as HTML.

<html> <head> <title>Strings</title> </head> <body>
<script type="text/javascript"> String.prototype.htmlEntities = function () { return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace (/>/g,'&gt;'); }; var tmp = '<html><head></head>'; var safe= tmp.htmlEntities(); document.write(safe); </script>
<p> This is the other way around, showing html tags' by simply using the "&lt;code&gt;&lt;/code&gt;" tags. <code>&lt;p&gt; some example of common tag's and p tags' is one of them&lt;/p&gt;</code>

07-25-2008, 09:47 PM
You'll want to format your code using the CODE tag, just for an easier read.