Jump to content
Sign in to follow this  
Es_Dexter

Add a no-repeat function to random page refresh

Recommended Posts

Hello,

I'm currently working on a school project involving a bunch of coding in html, css and js. The project's purpose is to basically build a website containing a 'quiz' system. I've already managed to successfully create one, but I'm not yet fully satisfied with it. The quiz contains 21 questions in total, and each question has their own html page; 21 pages in total. Now I have been using a java code to call a random page when a button is pressed on the home website (index.html). It works properly, but it often redirects me to the same page every time I click the button. I want to prevent it from visiting the same page whenever the button is pressed, until all 21 pages have been visited, at which point it resets the data and starts over.

I have been looking for a kind of script for this, but can't seem to find it. My only option would be to ask for help, which is why I'm here. :Pleased:

This is the code I am currently using for the page randomizer:

                        var howMany = 20; // max number of items listed below                        var page = new Array(howMany+1);                        page[0]="spelopdracht1.html";                        page[1]="spelopdracht2.html";                        page[2]="spelopdracht3.html";                        page[3]="spelopdracht4.html";                        page[4]="spelopdracht5.html";                        page[5]="spelopdracht6.html";                        page[6]="spelopdracht7.html";                        page[7]="spelopdracht8.html";                        page[8]="spelopdracht9.html";                        page[9]="spelopdracht10.html";                        page[10]="spelopdracht11.html";                        page[11]="spelopdracht12.html";                        page[12]="spelopdracht13.html";                        page[13]="spelopdracht14.html";                        page[14]="spelopdracht15.html";                        page[15]="spelopdracht16.html";                        page[16]="spelopdracht17.html";                        page[17]="spelopdracht18.html";                        page[18]="spelopdracht19.html";                        page[19]="spelopdracht20.html";                        page[20]="spelopdracht21.html";                                                function rndnumber(){                        var randscript = -1;                        while (randscript < 0 || randscript > howMany || isNaN(randscript)){                        randscript = parseInt(Math.random()*(howMany+1));                        }                        return randscript;                        }                        quo = rndnumber();                        quox = page[quo];                        window.location=(quox);                        }                        }

If there is anything you can do, please let me know. I would really appreciate your help! ^.^

Share this post


Link to post
Share on other sites

You can find out which page you're on using the location object. When the random number shows up, check whether the page associated with it is the same as the page you're currently on. If so, then choose another random number.

Share this post


Link to post
Share on other sites

You can use the localStorage API to store pages you've already visited.

//set page to an empty array, simpler syntaxvar page = [];page[0]="spelopdracht1.html";page[1]="spelopdracht2.html";page[2]="spelopdracht3.html";page[3]="spelopdracht4.html";page[4]="spelopdracht5.html";page[5]="spelopdracht6.html";page[6]="spelopdracht7.html";page[7]="spelopdracht8.html";page[8]="spelopdracht9.html";page[9]="spelopdracht10.html";page[10]="spelopdracht11.html";page[11]="spelopdracht12.html";page[12]="spelopdracht13.html";page[13]="spelopdracht14.html";page[14]="spelopdracht15.html";page[15]="spelopdracht16.html";page[16]="spelopdracht17.html";page[17]="spelopdracht18.html";page[18]="spelopdracht19.html";page[19]="spelopdracht20.html";page[20]="spelopdracht21.html";//get our data from local storage, it's a stringvar pagesAlreadyVisited = window.localStorage.getItem('pagesAlreadyVisited');if (pagesAlreadyVisited){    //convert comma delimited list to array    //eg. "4,5,9" to [4,5,9]    pagesAlreadyVisited = pagesAlreadyVisited.split(",");    //iterate through each page we already visited    pagesAlreadyVisited.forEach( function(pageIndex){       //remove this page from pages since we don't want to visit it       //splice removes it from the array completely       //play around with splice to understand how it works, its tricky!       page.splice(pageIndex,1);    });} else { //if pagesAlreadyVisited is null, empty or undefined  pagesAlreadyVisited = [];//set to empty array, we may use it later}//moved howMany down here, after all pages processing is completehowMany = page.length;function getRandPage(){    //get our random page, use Math.floor instead of parseInt.    var randIndex = Math.floor(Math.random()*howMany);    var randPage = page[randIndex];        //add page index to visited array    pagesAlreadyVisited.push(randIndex);    //convert array to string so we can store in localStorage    var pagesAlreadyVisitedString = pagesAlreadyVisited.join();    window.localStorage.setItem('pagesAlreadyVisited',pagesAlreadyVisitedString);        return randPage;}quox = getRandPage();window.location=(quox);console.log(pages.length);//number of unread pages remaining                        

Browser compatibility for localStorage is great these days:http://caniuse.com/#feat=namevalue-storage More resources:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStoragehttps://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

Edited by alexpnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×