(跨站漏洞轉義)JS及JQuery對Html內容編碼,Html轉義
阿新 • • 發佈:2019-01-29
-
原文 http://blog.csdn.net/phantomes/article/details/26570113
/** JQuery Html Encoding、Decoding * 原理是利用JQuery自帶的html()和text()函式可以轉義Html字元 * 虛擬一個Div通過賦值和取值來得到想要的Html編碼或者解碼 */ <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script type="text/javascript"> //Html編碼獲取Html轉義實體 function htmlEncode(value){ return $('<div/>').text(value).html(); } //Html解碼獲取Html實體 function htmlDecode(value){ return $('<div/>').html(value).text(); } </script>
<script type="text/javascript"> //獲取Html轉義字元 function htmlEncode( html ) { return document.createElement( 'a' ).appendChild( document.createTextNode( html ) ).parentNode.innerHTML; }; //獲取Html function htmlDecode( html ) { var a = document.createElement( 'a' ); a.innerHTML = html; return a.textContent; }; </script>
//編碼 function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, ">"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, "<br>"); return s; } //解碼 function html_decode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/>/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); s = s.replace(/<br>/g, "\n"); return s; }