javascript htmlspecialchars
记录:
<script> function htmlspecialchars(string){ var data = []; for(var i = 0 ;i <string.length;i++) { data.push( "&#"+string.charCodeAt(i)+";"); } return data.join(""); } document.write(htmlspecialchars("@{}<script>javascript alert<\/script>")); </script>
<script> function filter (str) { str = str.replace(/&/g, '&'); str = str.replace(/</g, '<'); str = str.replace(/>/g, '>'); str = str.replace(/'/g, '´'); str = str.replace(/"/g, '"'); str = str.replace(/\|/g, '¦'); return str; } alert(filter("url")) </script>
转载请注明:woyigui's blog [http://www.woyigui.cn/]
本文标题:javascript htmlspecialchars
本文地址:http://www.woyigui.cn/2009/09/25/javascript-htmlspecialchars/
赞join
[回复]
woyigui 回复:
九月 26th, 2009 at 上午 3:40
感谢大牛光临。。
[回复]
还有两个更好的技巧escapeHTML: function(str) { var div = document.createElement('div'); var text = document.createTextNode(str); div.appendChild(text); return div.innerHTML;}unescapeHTML: function(str) { var div = document.createElement('div'); div.innerHTML = str.replace(/</?[^>]+>/gi, ''); return div.childNodes[0] ? div.childNodes[0].nodeValue : '';}详细查看 http://www.gracecode.com/archives/2873/
[回复]
woyigui 回复:
九月 26th, 2009 at 上午 3:50
thx…
[回复]
楼下unescapeHTML这个方法,你可以44处理这样的数据:
<iframe>
所以,算是更好的技巧吗 : )
[回复]
[回复]