2014-10-18 3 views
0

버튼을 누르면 임의의 웹 페이지 (나는 35 개가 있음)를 선택하는 웹 사이트를 만들려고합니다. 그러나 나는 한 번만 각 사이트에 가고 싶습니다. 나는 쿠키가 그 일을 할 수 있다고 생각하지만 어떻게해야하는지 확신 할 수 없다. 그래서 어떤 페이지를 방문했는지, 어떤 페이지를 방문했는지 기억하지 못하도록 쿠키를 저장하려고합니다. JavaScript에 익숙하지 않으므로 잘 보내주십시오.어떤 웹 페이지를 방문했는지 기억하고 있습니다.

내 자바 스크립트 : 내 웹 페이지의

function myFunction() { 

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

하나 :

<!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> 

답변

0

당신이 방문한 페이지의 배열을 설정하고 읽을이 두 가지를 사용할 수 있습니다 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; 
} 

에서 촬영 :

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(',')) 
} 

while 루프는 실행에 약간의 시간이 걸릴 수 있지만, 특히 35 개의 가능한 페이지 (예 : 5 개의 숫자 만 남았을 때). 선택 목록에서 저장된 번호를 제거하여 코드를보다 효율적으로 만들 수 있습니다.

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(',')) 
} 
관련 문제