[zt]客服端的HTMLdecode和HTMLencode---in javascript
来源:互联网 发布:linux closehandle 编辑:程序博客网 时间:2024/05/16 01:22
今天碰到一个问题,在要通过ajax实现分页,需要对ajaxMethod返回的Html代码端重新组装,一段是分页,另一段是查询的结果,这样问题就来,如何分离这两端代码?我想到的办法就是在ajaxMethod里构造一个javascript对象,把这两段代码加到对象的属性值中如此模样
“//construct javascript object
var objHTML = new Object();
objHTML.firstSection = ?;
objHTML.secondSection = ?;
"
然后在前台的callback函数中通过eval(strCode)构造返回的javascript对象,接下来更新相应的HTML块就非常简单啦。var objHTML = new Object();
objHTML.firstSection = ?;
objHTML.secondSection = ?;
"
在我替换上面代码中的问号处,本来是换上两段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实现:{
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;
}
这样实现很方便简单,下面是示例代码供参考{
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','<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(HTMLDecode(strInput));
}
</script><button onclick="test1();">test HTMLEncode</button>
<button onclick="test2();">test HTMLDecode</button>
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','<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(HTMLDecode(strInput));
}
</script><button onclick="test1();">test HTMLEncode</button>
<button onclick="test2();">test HTMLDecode</button>
- [zt]客服端的HTMLdecode和HTMLencode---in javascript
- javascript HTMLEncode HTMLDecode的完整实例(兼容ie和火狐)
- javascript HTMLEncode HTMLDecode的完整实例(兼容ie和火狐)
- Server.Htmlencode和Server.HtmlDecode的用法
- Server.Htmlencode和Server.HtmlDecode的用法
- HtmlDecode 、HtmlEncode的用法
- 兼容ie和火狐的javascript HTMLEncode HTMLDecode的完整实例
- HtmlEncode与HtmlDecode的区别
- Server.HtmlEncode 與 Server.HtmlDecode 的用法
- 兼容IE和FF的JS HTMLEncode和HTMLDecode的完整实例
- javascript另类方法高效实现htmlencode()与htmldecode()函数
- HTMlEncode和HTMLDecode、UrlEncode和 UrlDecode 定义和用法
- HTMlEncode和HTMLDecode、UrlEncode和 UrlDecode 定义和用法
- HtmlEncode 以及HtmlDecode 方法
- Java HTMLEncode & HTMLDecode
- Java HTMLEncode & HTMLDecode
- ExtJs 2.1 中的htmlEncode和htmlDecode不可逆
- ckeditor使用及 Server.HtmlEncode与Server.HtmlDecode的区别
- 用CSS样式如何制作圆角的详细教程
- 对天乙社区bbscs8实现的详细分析二十三
- 教你如何制作精美的圆角表格
- 对天乙社区bbscs8实现的详细分析二十四
- 对天乙社区bbscs8实现的详细分析二十六
- [zt]客服端的HTMLdecode和HTMLencode---in javascript
- SQL小工具--移动数据库物理文件
- ORACLE XDK Reference
- zz 基于@AspectJ配置Spring AOP(之二)
- Java中利用正规表现判断E-mail的范例代码
- Blog 1(just for test)
- ASC码对照表
- Tomcat5.5.xx 数据库池简单配置 和Tomcat5.0.xx Web服务器数据库池简单配置
- oralce 中kill process 对应进程 小脚本