无聊写了一个将任意字符转换为ascii编码的页面,当输入字符串时,将自动转换成ascii编码,完整代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>字符串转换Ascii</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
function showKeyCode()
{
var Str=document.characterCode.character.value;
var character;
var code;
var msg = ""
for(var i=0;i<Str.length;i++)
{
character = Str.substring(i,i+1);
code = Str.charCodeAt(i);
msg+="&#"+code+";";
}
document.characterCode.result.value=msg;
}
function CopyResult()
{
if(document.characterCode.result.value!="")
{
copy_clip(document.characterCode.result.value);
}
}
function copy_clip(copy){
if (window.clipboardData){
window.clipboardData.setData("Text", copy);}
else if (window.netscape){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;
var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
if (!trans) return;
trans.addDataFlavor('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
var copytext=copy;
str.data=copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid=Components.interfaces.nsIClipboard;
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);}
alert("复制成功!")
return false;
}
</script>
<style type="text/css">
<!--
#Main {
width: 200px;
}
-->
</style>
<div id="Main">
<form name="characterCode">
请输入字符串:
<textarea name="character" cols="100" rows="5" οnkeyup="showKeyCode();" οnchange="showKeyCode();" οnblur="showKeyCode();"></textarea>
转换结果:
<br>
<textarea name="result" cols="100" rows="5"></textarea>
<input type=button value="复制" οnclick="CopyResult()">
</form>
</div>
</body>
</html>
运行效果: