Ok, my code is kind of messy, as I'm still a student learning the ropes of Javascript and HTML, so I'll try to explain as best I can.
I'm trying to get a "continue" link on several html pages to link to a randomly selected page from an array. I want that page to be taken out of the array, then the array to be saves in sessionStorage so that the next page's "continue" link will randomly select a link from the array that is NOT the current page, or past pages.
Here is my code:
var initialLinks=['music.html', 'test.html', 'recover.html', 'random.html']
function randomLink(){
if (typeof(Storage)!=="undefined")
{
if (sessionStorage.links)
{
var storedLinks = JSON.parse(sessionStorage.links);
var arraylink = storedLinks[Math.floor(Math.random()*storedLinks.length)];
window.location = arraylink;
var index = storedLinks.indexOf(arrayLink);
if (index > -1){
storedLinks.links.splice(index, 1);
}
sessionStorage.links = JSON.stringify(storedLinks);
}
else
{
sessionStorage.links = JSON.stringify(initialLinks);
var storedLinks = JSON.parse(sessionStorage.links);
var arraylink = storedLinks[Math.floor(Math.random() *storedLinks.length)];
window.location = arraylink;
var index = storedLinks.indexOf(arrayLink);
if (index > -1){
storedLinks.links.splice(index, 1);
}
sessionStorage.links = JSON.stringify(storedLinks);
}
}
else
{
document.write("Sorry, your browser does not support web storage.");
}
}
I'm getting my "continue" link to link to random pages, but the pages seem to remain in the array. Any help would be greatly appreciated. Also, I apologize in advance for my sloppy style :)
Looking closely, i noticed some other errors as well - arrayLink is written with a lower case L in window.location=arraylink, but in upper case elsewhere. Also, storedLinks.links.splice(index, 1); should be storedLinks.splice(index, 1); (without .links). And, once you've visited all your pages, you'll try to load an "undefined" page since you don't reinitialize your array.
This works for me (remember to save it to all your file names):