刚才本来打算把这个工具的代码贴在这里的,可是忙活了半天,却没有贴成功,真让人泄气。来CSDN.NET安家是这两天的事情,还不是太熟悉它的操作环境。只好再熟悉一下,慢慢来了。
这是一款使用XHTML+JavaScript写的XML/(X) HTML编解码程序。实现的功能其实很简单,或者叫做简陋,不过有时候它的确是一个不错的工具。尤其当需要把代码写入XHTML页面的时候,当然不能直接把代码嵌入到页面,这时候这个工具就派上了用场。
代码刚才没有贴上来,只好当作资源上传到资源中心了。这么简陋的东东上传到资源中心,我也觉得有点不合适,不过当时只想着要把代码共享了,也只有那么做了。
这个工具还是很有价值的,尤其提供了直接从文件中读取的功能。在IE中运行的时候,顶部的操作部分可以直接从硬盘的文件中读取源码,然后进行编解码工作,比复制->粘贴操作要方便一些。
现在既然熟悉了CSDN的操作环境,还是把代码放在这里。
JavaScript部分如下:
/**/
/*
*【File】
* Redfishx_XHTMLCoder.js
*【Author】
* 红鱼X (http://blog.csdn.net/redfishx)
*【Start Date】
* 2006年12月25日
*【Update】
* 2007年8月8日
*【Description】
* 提供一个类,Redfishx_XHTMLCoder。
* 该类封装两个静态成员函数,执行对XML/(X)TML的相应的编码解码功能。
* Redfishx_XHTMLCoder.HTMLEncode执行编码,即对XML/(X) HTML进行文本化
* Redfixhx_XHTMLCoder.HTMLDecode执行解码,执行相反操作。
*/
Redfishx_XHTMLCoder
=
new
Object();
Redfishx_XHTMLCoder.HTMLEncode
=
function
(htmlCode)
...
{
var gtChar = />/g;
var ltChar = /</g;
var quotChar = /"/g;
var andChar = /&/g;
var result = htmlCode.replace(andChar, "&");//先替换&,若放在后面,会把><"中的&替换掉
//可能是一个BUG,如果本行是代码的话,代码不运行window.alert("Type Of result:" + typeof(result));
result = result.replace(quotChar, """);
result = result.replace(gtChar, ">");
result = result.replace(ltChar, "<");
return result;
}
Redfishx_XHTMLCoder.HTMLDecode = function(htmlCode)
{
var gtChar = />/g;
var ltChar = /</g;
var quotChar = /"/g;
var andChar = /&/g;
var result = htmlCode.replace(ltChar, "<");
//可能是一个BUG,如果本行是代码的话,代码不运行window.alert("Type Of result:" + typeof(result));
result = result.replace(quotChar, """);
result = result.replace(gtChar, ">");
result = result.replace(andChar, "&");//最后替换&,若放在前面,可能生成新的><",那样就会替换掉本来不是这些串的字符串
return result;
}
然后是XHTML部分
<
html
>
<
head
>
<
title
>
Redfishx_XHTMLCoder
</
title
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=utf-8"
/>
<
script
type
="text/javascript"
src
="Redfishx_XHTMLCoder.js"
></
script
>
<
script
type
="text/javascript"
>
...
function displayFileContent()
...{
if(displayFileContent.arguments.length != 1)
...{
window.alert("displayFileContent 参数个数有误!");
return;
}
var file = document.getElementById("filePath").value;//提供文件路径
var hFSO, hFile; //定义用于保存FileSystemObject 句柄 和 文件句柄的变量
var bEncode = displayFileContent.arguments[0];
try
...{
hFSO=new ActiveXObject("Scripting.FileSystemObject"); //创建对FSO对象的引用
hFile=hFSO.OpenTextFile(file); //打开文本文件
var text;
if(bEncode)
text = Redfishx_XHTMLCoder.HTMLEncode(hFile.ReadAll()); //读取文件内容,并对特殊字符编码
else
text = Redfishx_XHTMLCoder.HTMLDecode(hFile.ReadAll()); //读取文件内容,并对特殊字符解码
hFile.Close(); //关闭文件
showFileContent(text); //显示文件内容
}
catch(e)
...{
window.alert(file);
window.alert("Error Info:" + e.description);
}
}
function showFileContent()//打开一个新窗口,显示内容
...{
var text;
if(showFileContent.arguments.length != 1)
...{
window.alert("参数个数有误!");
return;
}
text = showFileContent.arguments[0];
var newwin=window.open('','',''); //打开一个窗口并赋给变量newwin。
newwin.opener = null; // 防止代码对论谈页面修改
newwin.document.write("<code><pre>" + text + "</pre></code>"); //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
newwin.document.close();
}
function encode()
...{
var oSrc = document.getElementById("textArea_source");
var oTarget = document.getElementById("textArea_target");
oTarget.value = Redfishx_XHTMLCoder.HTMLEncode(oSrc.value);
}
function decode()
...{
var oSrc = document.getElementById("textArea_source");
var oTarget = document.getElementById("textArea_target");
oTarget.value = Redfishx_XHTMLCoder.HTMLDecode(oSrc.value);
}
</
script
>
<
style
type
="text/css"
>
...
/**//* <![CDATA[ */
body
{...}{
text-align:center;
}
textarea
{...}{
width:80%;
height:300px;
}
/**//* ]]> */
</
style
>
</
head
>
<
body
>
<
form
>
<
input
type
="file"
id
="filePath"
/>
<
input
type
="button"
value
="Encoding"
onclick
="displayFileContent(true);"
/>
<
input
type
="button"
value
="Decoding"
onclick
="displayFileContent(false);"
/>
<
hr
/>
Source Text:
<
br
/>
<
textarea
id
="textArea_source"
></
textarea
><
br
/><
br
/>
<
input
type
="button"
value
="Encode"
onclick
="encode();"
/>
<
input
type
="button"
value
="Decode"
onclick
="decode();"
/><
br
/><
br
/>
Target Text:
<
br
/>
<
textarea
id
="textArea_target"
></
textarea
>
</
form
>
</
body
>
</
html
>
只要保留作者的签名,这片代码当然是可以供任何人(不滥用的人)随意传播使用的。希望对朋友们有所帮助。
2007年8月8日
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)