Probably not the most beautiful and it'd become very long if more than two are needed, but this is A solution.
So basically i create two localStorage and I first check if they are empty and if the key isn't the same as per the variable I am setting, and do this each time I load the page.
if(!localStorage.getItem("idA")) {
var thisId = "<?php echo $id; ?>";
localStorage.setItem("idA", thisId);
console.log("A " + localStorage.getItem("idA"));
} else {
console.log("A " + localStorage.getItem("idA"));
}
if(localStorage.getItem("idA") != "<?php echo $id; ?>") {
if(!localStorage.getItem("idB")) {
var thisId = "<?php echo $id; ?>";
localStorage.setItem("idB", thisId);
console.log("B " + localStorage.getItem("idB"));
} else {
console.log("A " + localStorage.getItem("idA"));
console.log("B " + localStorage.getItem("idB"));
}
}
UPDATE
Based on Keith Answer, to add unique values (see this reply on SO), this is what I did:
var thisId = "<?php echo $id; ?>";
var lines = localStorage.getItem("lines") ? JSON.parse(localStorage.getItem("lines")) : [];
var myIdString = JSON.parse(localStorage.getItem("lines"));
lines.push(thisId);
function unique(list) { var result = [];
$.each(list, function(i, e) {
if ($.inArray(e, result) == -1) result.push(e); });
return result;
}
console.log(unique(lines));