技术文章
当前位置:首页 > 技术文章

javascript中的unicode效果

<!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" dir="ltr" lang="zh-cn" xml:lang="zh-cn">
<head> <title>字符转换Unicode编码工具</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="zh-cn" />
<meta name="description" content="unicode转码工具, 一般用来做国际化的, 用UNICODE转后的字符码 很让容易让JAVA识别,因为JAVA本省就是 UNICODE编码风格 " />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-style-type" content="text/javascript" />
<script language='JavaScript'>
function doit() {
var str = source.value;
var tmp1 = "";
var tmp2 = "";
var tmp3 = "";
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) < 128) {
tmp1 += str.charAt(i);
tmp2 += str.charAt(i);
tmp3 += str.charCodeAt(i) + ",";
}
else {
tmp1 += "&#"+str.charCodeAt(i)+";";
tmp2 += "
\\u"+(str.charCodeAt(i)).toString(16);
tmp3 += str.charCodeAt(i) + ",";
}
}
dest1.value = tmp1;
dest2.value = tmp2;
dest3.value = tmp3;
}
function doAntiHtml() {
var str = dest1.value;
var tmp1 = "";
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) == 10) {
tmp1 += "<br/>";
}
else if (str.charCodeAt(i) == 13) {
}
else if (str.charAt(i) == '<') {
tmp1 += "&";
}
else if (str.charAt(i) == '>') {
tmp1 += "&";
}
else {
tmp1 += str.charAt(i);
}
}
var dd = window.open("");
dd.document.body.innerHTML = tmp1;
}
function doAntiJava() {
var str = dest2.value;
var tmp1 = "";
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) == 10) {
tmp1 += "<br/>";
}
else if (str.charCodeAt(i) == 13) {
}
else if (str.charAt(i) == '<') {
tmp1 += "&";
}
else if (str.charAt(i) == '\'') {
tmp1 += "
\\\'";
}
else if (str.charAt(i) == '\\') {
if (i < (str.length - 1) && str.charAt(i + 1) != 'u') {
tmp1 += "
\\\\";
}
else {
tmp1 += "\\";
}
}
else if (str.charAt(i) == '>') {
tmp1 += "&";
}
else {
tmp1 += str.charAt(i);
}
}
var dd = window.open("");
dd.document.write("<script>document.write('"+tmp1+"');");
dd.document.write("<");
dd.document.write("/script>");
}
</script>
<style type="text/css">
<!--
html,bod
{width: "738px"; margin:0; font:100%/120% Verdana, Arial, Helvetica, sans-serif, 宋体;
padding: 0; margin-align: center; }
td {font-weight: bold; color:#6DBEF5;}
a { text-decoration:none;}
a:hover{text-decoration:none; color:red;}
-->
</style>
</head>
<body> <div style='background-color: #effae4;'> &nbsp;</div>
<p align="center"><font color="#800000" face="楷体_GB2312" size="5">字符转换Unicode编码</font> </p>
<table cellspacing="10" cellpadding="0" width="550px" align="center" bgcolor="#E2F2FD" border="0">
<tbody>
<tr>
<td align="center" bgcolor="#F1F9FE">
<textarea id="source" rows="6" cols="60"></textarea> <br/>
源数据:<input type="button" value="编&nbsp;&nbsp;码" onclick="doit();"/><br/><br/>
<textarea id="dest1" rows="6" cols="60"></textarea></textarea> <br/>
HTML&nbsp;Unicode编码:<input type="button" value="反编码" onclick="doAntiHtml();"/> <br/><br/>
<textarea id="dest2" rows="6" cols="60"></textarea></textarea> <br/>
JAVA&nbsp;Unicode编码:<input type="button" value="反编码" onclick="doAntiJava();"/> <br/><br/>
<textarea id="dest3" rows="6" cols="60"></textarea> <br/>Unicode编码值:
</td></tr></tbody></table>
<div align=right style="COLOR: #15B308;"><i><tt>by bobo55&nbsp;&nbsp;&nbsp;<br>
Last updated:&nbsp;<script>document.write(document.lastModified)</script></tt></i>&nbsp;&nbsp;&nbsp;</div>
</body>
</html>