淺談Javascript Base64 加密解密
html代碼:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style type="text/css">
*{font-family: Consolas;font-style: italic}
.responsebox{width:900px;margin:10px auto;padding:10px;border:2px solid #366;border-radius: 10px 0 10px 0; text-align: center}
.responsebox input,.responsebox button{font-size: 30px;margin:5px;padding:5px;}
.spansuper{vertical-align: super;font-size: 14px}
.spanbottom{vertical-align: text-bottom;font-size: 12px;margin-left: -110px}
#showbox{width:900px;height:430px;border:5px solid #663;border-radius: 0 20px 0 20px;margin:10px auto;padding:8px;font-size: 20px}
</style>
</head>
<body>
<div class="responsebox">
<h1>Javascript Base64 Encode & Decode<span class="spansuper">veinyf@gmail.com</span><span class="spanbottom">2014-12-27 17:44</span></h1>
<input type="text" id="input">
<input type="checkbox" id="checkbox" checked="checked">Base64</input>
<button id="btn">Convert done !</button>
</div>
<div id="showbox"></div>
</body>
<script type="text/javascript">
/*javascript知識(shí):
*函數(shù):window.atob() window.btoa() unescape() escape() encodeURIComponent() decodeURIComponent()
*正則表達(dá)式清除首位空格:_string.replace(/(^\s*)|(\s*$)/g,"");
*
*CovertBase64orString自執(zhí)行函數(shù)
*inputid 輸入框id
*checkboxid 選擇框id
*btnid 按鈕id
*showid html顯示容器id,這里是一個(gè)div#showbox
*/
(function CovertBase64orString(inputid, checkboxid, btnid, showid) {
var checkbox = document.getElementById(checkboxid); //html dom select checkbox
var chkvalue = checkbox.getAttribute("checked"); //html dom select checkedvalue
var btn = document.getElementById(btnid); //html dom select button id
var isbase64; //base64toString or StringtoBase64 bool
var returnval = null; //Converted string
chkvalue == "checked" ? isbase64 = true : isbase64 = false; //判斷check按鈕初始化狀態(tài) 賦值isbase64
checkbox.addEventListener("click", function(e) { //checkbox 點(diǎn)擊事件注冊(cè)
var _ckvak = checkbox.getAttribute("checked"); //點(diǎn)擊事件發(fā)生時(shí),改變check狀態(tài),賦值isbase64
if (_ckvak == "checked") {
checkbox.setAttribute("checked", null);
isbase64 = false;
} else {
checkbox.setAttribute("checked", "checked");
isbase64 = true;
}
}, true);
btn.addEventListener("click", function(e) { //button 點(diǎn)擊事件注冊(cè)
var _show = document.getElementById(showid); //html dom select showbox id
var _inputvalue = document.getElementById(inputid).value; //文本框取值
//_inputvalue=_inputvalue.replace(/(^\s*)|(\s*$)/g, ""); //正則表達(dá)式去除首位空格,似乎btoa,abob已經(jīng)做了這些工作
var _showlength = _show.childNodes.length; //遍歷showbox,清除showbox內(nèi)容
while (_showlength > 0) {
_show.removeChild(_show.childNodes[_showlength - 1]);
_showlength--;
}
if (isbase64) { //string to base64,支持中文編碼,unescape,encodeURIComponent
returnval = window.btoa(unescape(encodeURIComponent(_inputvalue)));
} else { //base64 to string
returnval = decodeURIComponent(escape(window.atob(_inputvalue)));
}
_show.appendChild(document.createTextNode(returnval)); //add context to showbox
}, true);
})("input", "checkbox", "btn","showbox");
//CovertBase64orString("input", "checkbox", "btn","showbox");
</script>
</html>
效果:
推薦一個(gè)Javascript IDE 比Aptana還好用。Komodo IDE(免費(fèi)版:Komodo Edit,基本功能一樣)支持語(yǔ)法高亮,智能感知,還支持perl,python,ruby,nodejs語(yǔ)法等。
相關(guān)文章
layui實(shí)現(xiàn)點(diǎn)擊按鈕給table添加一行
想實(shí)現(xiàn)點(diǎn)擊按鈕在表格添加一行的功能,但發(fā)現(xiàn)layui并未集成該工具欄,因此,需要自己手動(dòng)添加這個(gè)功能;這篇文章主要介紹了layui點(diǎn)擊按鈕給table添加一行,需要的朋友可以參考下2018-08-08基于原生js實(shí)現(xiàn)九宮格算法代碼實(shí)例
這篇文章主要介紹了基于原生js實(shí)現(xiàn)九宮格算法代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07js保留兩位小數(shù)最簡(jiǎn)單的實(shí)現(xiàn)方法
JS數(shù)據(jù)格式化是在進(jìn)行web前端開(kāi)發(fā)時(shí)常碰到的事情,特別是在數(shù)據(jù)類(lèi)型為Float的數(shù)據(jù)就需要特殊處理,如保留兩位小數(shù)、小數(shù)點(diǎn)后的數(shù)據(jù)是否需要四舍五入等等,下面這篇文章主要給大家介紹了關(guān)于js保留兩位小數(shù)最簡(jiǎn)單的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-05-05微信小程序?qū)崿F(xiàn)驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)驗(yàn)證碼倒計(jì)時(shí)效果,手機(jī)登錄、填手機(jī)號(hào)獲取驗(yàn)證碼,倒計(jì)時(shí)后重新獲取效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05javascript實(shí)現(xiàn)滾動(dòng)效果的數(shù)字時(shí)鐘實(shí)例
這篇文章主要是介紹使用javascript來(lái)實(shí)現(xiàn)數(shù)字時(shí)鐘滾動(dòng)的效果,非常實(shí)用,有需要的朋友們可以來(lái)參考學(xué)習(xí)。2016-07-07JavaScript 實(shí)現(xiàn)類(lèi)的多種方法實(shí)例
JavaScript 實(shí)現(xiàn)類(lèi)的多種方法實(shí)例,需要的朋友可以參考一下2013-05-05bootstrap 日期控件 datepicker被彈出框dialog覆蓋的解決辦法
這篇文章主要介紹了bootstrap 日期控件 datepicker被彈出框dialog覆蓋的解決辦法 ,本文給大家分享幾種解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Three.js實(shí)現(xiàn)瀏覽器變動(dòng)時(shí)進(jìn)行自適應(yīng)的方法
這篇文章主要給大家介紹了關(guān)于Three.js實(shí)現(xiàn)瀏覽器變動(dòng)時(shí)進(jìn)行自適應(yīng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09