0

Encoding my URL works perfectly with base-64 encoding. So does decoding but not with the string literal variable.

This works:

document.write(atob("hi"));

This does not:

var tempvar = "hello";
document.write(atob(tempvar));

What am I doing wrong? Nothing is displayed. But if I quote "tempvar", then it of course works but is not the same thing since "tempvar" is a string, not a variable.

1
  • 1
    Browser returned you an exception. Any chance you have read the message? Commented Mar 30, 2017 at 22:08

4 Answers 4

1

Your Question

What am I doing wrong?

The string being passed to atob() is a string literal of length 5 (and not technically a base-64 encoded string). The browser console should reveal an exception in the error log (see explanation in The cause below).

The cause

Per the MDN documentation of atob():

Throws

Throws a DOMException if the length of passed-in string is not a multiple of 4. 1

The length of the string literal "hello" (i.e. 5) is not a multiple of 4. Thus the exception is thrown instead of returning the decoded version of the string literal.

A Solution

One solution is to either use a string that has actually been encoded (e.g. with btoa()) or at least has a length of four (e.g. using String.prototype.substring()). See the snippet below for an example.

var tempvar = "hello";
window.addEventListener("DOMContentLoaded", function(readyEvent) {
    var container = document.getElementById("container");
    //encode the string
    var encoded = btoa(tempvar); 
    container.innerHTML = encoded;

    var container2 = document.getElementById("container2"); 
    //decode the encoded string
    container2.innerHTML = atob(encoded);  
    
    var container3 = document.getElementById("container3");
    //decode the first 4 characters of the string
    container3.innerHTML = atob(tempvar.substring(0, 4));
});
<div> btoa(tempvar): <span id="container"></span></div> 
<div> atob(decoded): <span id="container2"></span></div>
<div> atob(tempvar.substring(0, 4)): <span id="container3"></span></div> 


1https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, my bad. My example was not the literal problem that I am facing, it was just an example that a variable will not work as it should. My real variable is "totalprice" and when you click for example a button that has totalprice set to 10, the URL will be something like: "transaction.html?totalprice=10" and this is not working. It doesn't know what is "totalprice=10" but in base64encode.org it works without any problems. Sorry for the very late response, I have had some problems and could've not been here to develop my script. Thanks thought, and ideas?
Are you trying to encode using atob()? That function is used for decoding... instead use btoa() to encode...
Yes, I use this to make the url encoded:<script> var a = document.getElementById('50m'); a.href = "transaction.html?" + btoa("totalprice=50"); </script> but it doesn't work when trying to decode. EDIT: Idk but btoa() works but not atob() in that script.
1

It's because it can't decode the string "hello", try an actual string that can be decoded from base64, here is an example;

var tempvar = "aHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucy80MzEyOTEzNi9kZWNvZGluZy1ub3Qtd29ya2luZy13aXRoLWJhc2U2NA==";
document.write(atob(tempvar));

If you want to encode, use the btoa function instead,

var tempvar = "hello";
document.write(btoa(tempvar));

You can use this website to test decoding and encoding base64, https://www.base64encode.org/

Comments

0

it's because you are trying to decode a not base64 encoded string that it works on hi is just a coincidence it seems.

atob = decode

btoa = encode

Comments

0

You're using the wrong function. You should use btoa() to encode.

When you do atob('hi'), you're actually decoding 'hi', which happens to be valid base-64.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.