2017-01-22 106 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

由于它的做工精细。 –