javascript教程 html非法字元過濾與轉換函數
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head>
<meta HTTP-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript html非法字元過濾與轉換函數</title>
<script language="javascript">
function htmlspecialchars(str) {
return preg_replace(['&', '<', '>', '"'], ['&amp;', '&lt;', '&gt;', '&quot;'], str);
}
function enhtml(str)
{
return preg_replace(['&nbsp;', '&lt;', '&gt;', '&amp;'], [' ', '<', '>', '&'], str);
}
function htmltohtml()
{
$("t2").value =htmlspecialchars($("t1").value);
}
function htmltoA()
{
$("t1").value =htmlspecialchars($("t2").value);
}
function $(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<p> </p>
<form id="form1" name="form1" method="post" action="">
<label>
<textarea name="t1" cols="" rows="" id="t1"></textarea>
<br />
<br />
<input type="button" name="Submit" value="轉換成html編碼" onclick="htmltohtml();" />
</label>
<label>
<input type="button" name="Submit2" value="轉換成html標籤" onClick="htmltoA();" />
</label>
<p>
<textarea name="t2" cols="" rows="" id="t2"></textarea>
</p>
</form>
</body>
</html>