I have some JavaScript code which provides a random link from preselected links. It stores links you visited and doesn't provide you the link you already visited. When all links are visited it deletes its visited sites data.
// Store javascript object to localStorage
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
// Get javascript object from localStorage
Storage.prototype.getObject = function(key) {
return JSON.parse(this.getItem(key));
}
// Your URLs with default visited values
var urls = [
{ name: "somesite1", url: "http://somesite1.com", visited: false },
{ name: "somesite2", url: "http://somesite2.com", visited: false },
{ name: "somesite3", url: "http://somesite3.com", visited: false },
{ name: "somesite4", url: "http://somesite4.com", visited: false }
];
// If there's no urls object in localStorage, call setDefault method
if (!localStorage.getObject("urls")) {setDefault();}
// Check all link objects. If all are visited, return true, else return false
function checkVisited() {
var counter = 0;
var getUrls = localStorage.getObject("urls");
for (var i = 0; i < getUrls.length; i++) {
if (getUrls[i].visited) {counter++;}
}
return counter === getUrls.length;
}
// Set defaults values to localStorage
function setDefault() {
localStorage.setObject("urls", urls);
}
// If all links are visited, set default values
// Then get random links until you find one
// that's not visited. When it's found, set it
// to visited in localStorage and redirect to it
function goSomewhere() {
if (checkVisited()) {setDefault();}
var getUrls = localStorage.getObject("urls");
var visited = true;
while(visited) {
var e = Math.floor(Math.random()*getUrls.length);
if (!getUrls[e].visited) {
visited = false;
getUrls[e].visited = true;
localStorage.setObject("urls", getUrls);
window.location = getUrls[e].url;
}
}
}
<input class="start" type="button" onClick="goSomewhere(); return ;" alt="Submit" width="800" height="100"value="»» Bring Links ««"
What i want to do is add a checkbox to my html, if it's checked then another preselected links set that i'm going to create will be used. Otherwise it will use the same links.