辋川集

木末芙蓉花,山中发红萼,涧户寂无人,纷纷开且落

By - 陳 思敬

同事写的JavaScript转码小工具

同事作品,分享给经常转码的朋友。
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。
unescape() 函数可对通过 escape() 编码的字符串进行解码。

奉上代码,感谢主任:

<!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=gb2312" />
<title>测试</title>
<style type="text/css">
    .button{width:170px;height:39px}
    textarea,p{float:left}
    textarea{width:420px;height:250px;}
    p input{margin-bottom:5px}
</style>
</head>
<body>
<textarea id="code" rows="16" cols="46"></textarea>
<p style="text-align:center;width:200px">
    <input type="button" value="escape >>" id="submit0" class="button"> <br />
    <input type="button" value="<< unescape" id="submit1" class="button"> <br /><br />
    <input type="button" value="encodeURIComponent >>" id="submit2" class="button"> <br />
    <input type="button" value="<< decodeURIComponent" id="submit3" class="button"> <br />
</p>
<textarea id="code2" rows="16" cols="66"></textarea><br>
<script>
var target = document.querySelector("#code"),
    target2 = document.querySelector("#code2"),
    tags = document.querySelectorAll(".button");
tags[0].onclick = function(){
    target2.value = escape(target.value);
}
tags[1].onclick = function(){
    target.value = unescape(target2.value);
}
tags[2].onclick = function(){
    target2.value = encodeURIComponent(target.value);
}
tags[3].onclick = function(){
    target.value = decodeURIComponent(target2.value);
}
target.onpaste = function(){
    target2.value = '';
}
target2.onpaste = function(){
    target.value = '';
}
</script>
</body>
</html>

Leave a Reply

Your email address will not be published.
*
*