0

I am trying to make a website where it picks a random webpage ( i have 35) when you press on a button. However I want it to only go to each site once. I am thinking that a cookie could do the job but I am unsure how to do it. So I am trying to have cookies remember which pages have been visited and which have not. I am quite new to JavaScript so please be nice.

My JavaScript:

function myFunction() {

var tal=Math.floor((Math.random() * 35) + 1)
 window.location.href = "Sang"+tal+".html";
 }

One of my webpages:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Gæt en sang</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="Test.js"></script>
</head>

<body>
    <audio class="hidden" controls autoplay>
  <source src="horse.ogg" type="audio/ogg">
  <source src="pokemon.mp3" type="audio/mpeg">
    </audio>
        <div id="header">
            <h2> Gæt sangen og hejs flagstangen!</h2>
        </div>
        <div id="left">
        <ul> Hvilken sang er dette?

            </br>
            <button type="button" onclick="myFunction()">
            <li> World we must defend </li>
            </button>
            </br>
            <button type="button" onclick="myFunction()">
            <li> Pokemon theme</li>
            </button>
            </br>
            <button type="button" onclick="myFunction()">
            <li> Gotta Catch 'em All<li>
            </button>
            </br>
            <button type="button" onclick="myFunction()">
            <li> You teach me i teach you <li>
    </button>
        </ul>
        </div>
   </div>
    <p id="Test"></p>
</body>

</html>

1 Answers1

0

Taken from https://stackoverflow.com/a/24103596/1029988

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

you can use these two to set and read an array of pages visited:

function myFunction() {
    var cookie = readCookie('pages') || '';
    var pages = cookie.split(',');
    var tal=Math.floor((Math.random() * 35) + 1);

    while (pages.indexOf(tal) > -1) { // randomize until you reach a number not stored in the cookie
        tal=Math.floor((Math.random() * 35) + 1);
    }

    window.location.href = "Sang"+tal+".html";
    pages.push(tal);
    createCookie('pages', pages.join(','))
}

the while loop could take some time to execute, though, especially towards the end of the 35 possible pages (i.e. when only 5 number are left). You can make the code more efficient by removing the stored numbers from the list of choices:

function myFunction() {
    var cookie = readCookie('pages') || '';
    var pages = cookie.split(',');

    var available_choices = [];

    for (var i = 1; i < 36; i++) {
        if (pages.indexOf('' + i) == -1) {
            available_choices.push(i);
        }
    }

    var tal=available_choices[Math.floor(Math.random() * available_choices.length - 1)];

    window.location.href = "Sang"+tal+".html";
    pages.push(tal);
    createCookie('pages', pages.join(','))
}
Community
  • 1
  • 1
Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42