I found out by myself. This code should do it if one is needs the to do the same:
for (var index = 0; index >= window.sessionStorage.key(index); index++) {
if (window.sessionStorage.key(index) === null) {
window.sessionStorage.setItem(index, JSON.stringify(cpData));
break;
} else if (index > window.sessionStorage.key(index)) {
window.sessionStorage.setItem(index, JSON.stringify(cpData));
break;
}
};
This time it checks wether or not window.sessionStorage.key(index)
isn't there. If so it will create a datastring. The break;
statement stops the loop immediately. Now the next time the button is pressed and the loop is started, it can see that there is already a datastring, it will then continue to loop until index
value is bigger than the key biggest key value at the moment. When it is bigger than al previous saved data, it will save the data and break;
the loop.
If one wants to check it by him/herself add this to the same script:
var i = 0, oJson = {}, sKey;
for (i; sKey = window.sessionStorage.key(i); i++) {
oJson[sKey] = window.sessionStorage.getItem(sKey);
}
console.log(oJson);
This will print out ALL sessionStorage data in console.