技术开发 频道

客服端的HTMLdecode和HTMLencode


【IT168技术文档】

  今天碰到一个问题,在要通过ajax实现分页,需要对ajaxMethod返回的Html代码端重新组装,一段是分页,另一段是查询的结果,这样问题就来,如何分离这两端代码?我想到的办法就是在ajaxMethod里构造一个javascript对象,把这两段代码加到对象的属性值中如此模样
//construct javascript object var objHTML = new Object(); objHTML.firstSection = ?; objHTML.secondSection = ?;
  然后在前台的callback函数中通过eval(strCode)构造返回的javascript对象,接下来更新相应的HTML块就非常简单啦。
  在我替换上面代码中的问号处,本来是换上两段HTML代码块字符串就可以啦,但我想如果在这里给他们加上encode,那么在前台如何decode呢,我试了在后台加上Server.URLEncode(strHtml),前台需要套uridecode()和unecape(),但里面的空格还是‘+’,在加一个replace(/\+/g,' ')就可以搞定啦,这样潜在的一个问题就是如果内容里面本来就有'+'会有问题的,于是我在网上搜了一下看看有没有更好的解决办法,结果发现一种很巧的办法,用HTML中的容器来实现。
  javascript HTMLencode实现:
function HTMLEncode(strHTML) { var div = document.createElement('div'); div.innerText = strHTML; return div.innerHTML; }
  javascript HTMLdecode实现:
function HTMLDecode(strEncodeHTML) { var div = document.createElement('div'); div.innerHTML = strEncodeHTML; return div.innerText; }
  这样实现很方便简单,下面是示例代码供参考
<script type="text/javascript"> function HTMLEncode(strHTML) { var div = document.createElement('div'); div.innerText = strHTML; return div.innerHTML; } function HTMLDecode(strEncodeHTML) { var div = document.createElement('div'); div.innerHTML = strEncodeHTML; return div.innerText; } function test1() { var strInput = prompt('test HTMLEncode(),please input decodeHTML','<div style=\"width:600px;\"><input type=\"button\" name=\"runStart\" value=\"View\" onclick=\"Run()\" style=\"margin-left:2px;\" /><input type=\"button\" name=\"runStart\" value=\"RunScript\" onclick=\"RunScript()\" style=\"margin-left:10px;\" /><input type=\"button\" name=\"reset\" value=\"Clear\" onclick=\"Reset()\" style=\"margin-left:18px;\" /><span onclick=\"ExpandCollapse(this);\" onmouseover=\"showMsg();\" onmouseout=\"hideMsg();\" msg=\"expand\" style=\"font-family:webdings;font-size:18pt;cursor:pointer;\">5</span></div><iframe name=\"ifrShow\" style=\"margin:0px;margin:0px;width:600px;height:400px;border:1px orange solid;\"></iframe><div id=\"divMsg\" style=\"display:none\"></div>'); alert(HTMLEncode(strInput)); } function test2() { var strInput=prompt('test HTMLDecode(),please input encodeHTML','&lt;div style="width:600px;"&gt;&lt;input type="button" name="runStart" value="View" onclick="Run()" style="margin-left:2px;"&gt;&lt;input type="button" name="runStart" value="RunScript" onclick="RunScript()" style="margin-left:10px;"&gt;&lt;input type="button" name="reset" value="Clear" onclick="Reset()" style="margin-left:18px;"&gt;&lt;span onclick="ExpandCollapse(this);" onmouseover="showMsg();" onmouseout="hideMsg();" msg="expand" style="font-family:webdings;font-size:18pt;cursor:pointer;"&gt;5&lt;/span&gt;&lt;/div&gt;&lt;iframe name="ifrShow" style="margin:0px;margin:0px;width:600px;height:400px;border:1px orange solid;"&gt;&lt;/iframe&gt;&lt;div id="divMsg" style="display:none"&gt;&lt;/div&gt;'); alert(HTMLDecode(strInput)); } </script> <button onclick="test1();">test HTMLEncode</button> <button onclick="test2();">test HTMLDecode</button>
0
相关文章