Friday, May 8, 2009

HTML Escaper Script

<script type="text/javascript">
/*
Simple HTML escaper by Raymond May Jr.
Uses global regex to replace < and > with HTML entities.
http://www.compender.com
*/

function escape()
{
var input = document.getElementById("input").value;

input = input.replace(/&/g,"&amp;");

input = input.replace(/</g,"&lt;");
input = input.replace(/>/g,"&gt;");

document.getElementById("input").value = input;
}

function unEscape()
{
var input = document.getElementById("input").value;

input = input.replace(/&lt;/g,"<");
input = input.replace(/&gt;/g,">");

input = input.replace(/&amp;/g,"&");

document.getElementById("input").value = input;
}
</script>

<html>
<body>
<h2>HTML Escaper</h2>
<form>
<textarea id="input" rows="30" cols="80"></textarea>
<br />
<input type="button" value="Escape!" onClick="escape()" />
<input type="button" value="Un-Escape!" onClick="unEscape()" />
</form>
</body>
</html>

No comments:

Post a Comment