html代码: 复制代码 代码如下: Page Title <br /> *{font-family: Consolas;font-style: italic}<br /> .responsebox{width:900px;margin:10px auto;padding:10px;border:2px solid #366;border-radius: 10px 0 10px 0; text-align: center}<br /> .responsebox input,.responsebox button{font-size: 30px;margin:5px;padding:5px;}<br /> .spansuper{vertical-align: super;font-size: 14px}<br /> .spanbottom{vertical-align: text-bottom;font-size: 12px;margin-left: -110px}<br /> #showbox{width:900px;height:430px;border:5px solid #663;border-radius: 0 20px 0 20px;margin:10px auto;padding:8px;font-size: 20px}<br /> Javascript Base64 Encode & Decodeveinyf@gmail.com2014-12-27 17:44 Base64 Convert done ! <br /> /*javascript知识:<br /> *函数:window.atob() window.btoa() unescape() escape() encodeURIComponent() decodeURIComponent()<br /> *正则表达式清除首位空格:_string.replace(/(^\s*)|(\s*$)/g,"");<br /> *<br /> *CovertBase64orString自执行函数<br /> *inputid 输入框id<br /> *checkboxid 选择框id<br /> *btnid 按钮id<br /> *showid html显示容器id,这里是一个div#showbox<br /> */<br /> (function CovertBase64orString(inputid, checkboxid, btnid, showid) {<br /> var checkbox = document.getElementById(checkboxid); //html dom select checkbox<br /> var chkvalue = checkbox.getAttribute("checked"); //html dom select checkedvalue<br /> var btn = document.getElementById(btnid); //html dom select button id<br /> var isbase64; //base64toString or StringtoBase64 bool<br /> var returnval = null; //Converted string<br /> chkvalue == "checked" ? isbase64 = true : isbase64 = false; //判断check按钮初始化状态 赋值isbase64<br /> checkbox.addEventListener("click", function(e) { //checkbox 点击事件注册<br /> var _ckvak = checkbox.getAttribute("checked"); //点击事件发生时,改变check状态,赋值isbase64<br /> if (_ckvak == "checked") {<br /> checkbox.setAttribute("checked", null);<br /> isbase64 = false;<br /> } else {<br /> checkbox.setAttribute("checked", "checked");<br /> isbase64 = true;<br /> }<br /> }, true);<br /> btn.addEventListener("click", function(e) { //button 点击事件注册<br /> var _show = document.getElementById(showid); //html dom select showbox id<br /> var _inputvalue = document.getElementById(inputid).value; //文本框取值<br /> //_inputvalue=_inputvalue.replace(/(^\s*)|(\s*$)/g, ""); //正则表达式去除首位空格,似乎btoa,abob已经做了这些工作<br /> var _showlength = _show.childNodes.length; //遍历showbox,清除showbox内容<br /> while (_showlength > 0) {<br /> _show.removeChild(_show.childNodes[_showlength - 1]);<br /> _showlength--;<br /> }<br /> if (isbase64) { //string to base64,支持中文编码,unescape,encodeURIComponent<br /> returnval = window.btoa(unescape(encodeURIComponent(_inputvalue)));<br /> } else { //base64 to string<br /> returnval = decodeURIComponent(escape(window.atob(_inputvalue)));<br /> }<br /> _show.appendChild(document.createTextNode(returnval)); //add context to showbox<br /> }, true);<br /> })("input", "checkbox", "btn","showbox");<br /> //CovertBase64orString("input", "checkbox", "btn","showbox");<br /> 效果: 推荐一个Javascript IDE 比Aptana还好用。Komodo IDE(免费版:Komodo Edit,基本功能一样)支持语法高亮,智能感知,还支持perl,python,ruby,nodejs语法等。