2017-01-22 103 views
2

我有此憑據:的Javascript Base64編碼

username: pippo 
password: 1234\zxcv 

我需要生成基64編碼。正確的基地64串

我使用BTOA(),但不能正確轉換字符串。 問題是\字符。何我可以逃脫它?

由於

回答

0

逃逸\\\當用戶輸入字符\

<form> 
 
username: <input type="text" /><br /> 
 
password: <input type="password" /><br /> 
 
<input type="submit" /> 
 
</form> 
 
<script> 
 
    var input = document.querySelector("input[type=password]"); 
 
    input.oninput = function(e) { 
 
    if (e.target.value.slice(-1) === "\\") { 
 
     e.target.value = e.target.value.replace(/\\/g, "\\"); 
 
    } 
 
    } 
 
    
 
    document.querySelector("input[type=submit]") 
 
    .onclick = function(e) { 
 
    e.preventDefault(); 
 
    console.log(btoa(input.value), atob(btoa(input.value))); 
 
    } 
 
</script>

+0

由於它的做工精細。 –