Lets say i have a JSON {"ID":"1","Name":"XYZ"}
I want to encode this data and send it to new page say new.html and display this data as ID: 1 Name: XYZ
. How can I achieve this?
Till now i've tried this:
url = 'new.html?' + encodeURIComponent(window.btoa(JSON.stringify(str)));
document.location.href = url;
This code is in my first.html script tag. In my new.html I tried this:
<div id='here'></div>
<script>
window.onload = function () {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
console.log(JSON.parse(window.atob(params)));
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
document.getElementById('here').innerHTML = data.id;
}
</script>
But in console.log i'm getting an error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.