Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cant get my cookies to work!

    hey there, below is the script for a java script quiz i made called quizpage.html. I want it to submit a cookie (the result of the quiz) to a page called datapage.html but i cant get it to work and ive tried everything...help?

    heres the quizpage code:

    <html>


    <script type="text/javascript"
    language="JavaScript 1.5"
    src="pd-cookie-functions.js">
    </script>

    <head>

    <title>Assignment</title>
    <p>
    <H1><b>Quiz</b></H1>

    <script>
    cookiename="quiz";
    function submitCookie()
    {
    SetCookie (cookiename,document.quiz.scorefield.value);
    location = "datapage.html";
    }

    </script>

    <form name = quiz
    action = "Javascript: submitCookie()" >
    <body>

    <script>

    var questions = new Array();
    questions[1] = "What country are Placebo from?";
    questions[2] = "Who released the debut album 'Hot Fuss'?";
    questions[3] = "What actress starred in Hackers the movie?";
    questions[4] = "Which now solo artist once fronted The Smiths?";
    questions[5] = "Which is the following is not a sleater-kinney album?";
    questions[6] = "Who recently released his debut album 'Destroy Rock and Roll?'";

    var answerone = new Array();
    answerone[1] = "Spain";
    answerone[2] = "Kerbdog";
    answerone[3] = "Natalie Portman";
    answerone[4] = "Damien Rice";
    answerone[5] = "Waiting";
    answerone[6] = "Mylo";

    var answertwo = new Array();
    answertwo[1] = "Luxembourg";
    answertwo[2] = "Nirvana";
    answertwo[3] = "Mena Suvari";
    answertwo[4] = "Morrissey";
    answertwo[5] = "Hot Rock";
    answertwo[6] = "Michael Gray";

    var answerthree = new Array();
    answerthree[1] = "France";
    answerthree[2] = "The Killers";
    answerthree[3] = "Angelina Jolie";
    answerthree[4] = "Paddy Casey";
    answerthree[5] = "All hands on the bad one";
    answerthree[6] = "David Morales";

    var rightanswers = new Array();
    rightanswers[1] = 2;
    rightanswers[2] = 3;
    rightanswers[3] = 3;
    rightanswers[4] = 2;
    rightanswers[5] = 1;
    rightanswers[6] = 1;

    var score = 0;

    function checkanswers()
    {

    score = 0;
    for (var i = 1; i <questions.length; i++)
    {
    if (document.quiz["answer" + i].value == rightanswers[i])
    score++;
    }
    document.quiz.scorefield.value = score + "/" + (questions.length-1);
    }
    </script>

    <script>

    for ( i = 1; i < questions.length; i++)
    {


    document.write ("<p>");
    document.write (questions [i]);
    document.write ("<p>");

    document.write ("<select name = answer" + i + ">");
    document.write ("<option value = 0>Choose...</option>");

    document.write ("<option value = 1>" + answerone[i] + "</option>");
    document.write ("<option value = 2>" + answertwo[i] + "</option>");
    document.write ("<option value = 3>" + answerthree[i] + "</option>");


    document.write ("</select>");
    }

    </script>

    <p>
    Score=

    <input type="text" name = "scorefield" value = "" size = "5">

    <input type = button value = "Score Quiz" onclick = "checkanswers();">

    <input type = "button" value = "Store Score" = onclick="submitCookie()">
    <p>
    <a href = "datapage.html">Back to Data Page</a>
    <p>
    </body>
    </form></html>





    heres the datapage code, where i want the cookie to be displayed:




    <script type="text/javascript"
    language="JavaScript 1.5"
    src="pd-cookie-functions.js">
    </script>


    <html>

    <head>

    <script type="text/javascript" language="JavaScript 1.5"src="pd-cookie-functions.js">
    </script>

    <title>Assignment</title>

    </head><H1>My Details</H1>
    <br>
    <br>
    <br>
    <script>
    var cookiename1="email";
    var cookiename2="quiz";
    var cookiename3="album";

    function GetEmail()
    {
    var emailname=GetCookie (cookiename1);
    if(emailname)
    document.write(emailname);
    else
    document.write ("none set");
    }

    function GetQuiz()
    {
    var quizname=GetCookie (cookiename2);
    if (quizname)
    document.write (quizname);
    else
    document.write ("none set");
    }

    function GetAlbum ()
    {
    var albumname=GetCookie (cookiename3);
    if (albumname)
    document.write(albumname);
    else
    document.write ("none set");
    }

    function Delete(index)
    {
    if (index==1)
    {DeleteCookie(cookiename1);}

    else if (index==2)
    {DeleteCookie(cookiename2);}

    else if (index==3)
    {DeleteCookie(cookiename3);}

    }

    </script>

    Email:
    <script>
    GetEmail()
    </script>
    <br>
    <a href="Emailpage.html">Set</a>
    <a href="Datapage.html" onclick="Delete(1)">Delete</a>
    <p>



    Quiz Result:
    <script>
    GetQuiz()
    </script>
    <br>
    <a href="Quizpage.html">Set</a>
    <a href="Datapage.html" onclick="Delete(2)"> Delete </a>

    <p>

    Album:
    <script>
    GetAlbum()
    </script>
    <br>
    <a href="Albumpage.html">Set</a>
    <a href="Datapage.html" onclick="Delete(3)">Delete</a?
    p>
    </a></html>

  • #2
    New Coder
    Join Date
    Mar 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First of all, let me start by saying this will be way easier with a serverside language like PHP, however, that is not the answer to your question.

    Now, onto your question.
    - Do you get javascript errors? If so, post them here
    - I assume you've tried if your SetCookie and GetCookie functions are correct (by for example setting and immediately getting the same cookie in one page)? If not, try it (i can't check those functions for you, because you use an external file, and you didn't post it)
    - Have you checked that both cookiename and document.quiz.scorefield.value contain the desired information. Maybe adding a line "alert(cookiename+'\n'+document.quiz.scorefield.value);" at the top of your submitCookie function will show if those values are correct.

    Another thing: you should use <script type="application/x-javascript" src="pd-cookie-functions.js"> instead of the language="whatever" thing.

    Also, please post your HTML/javascript between [code] tags, it's much easier to read.

  • #3
    New Coder
    Join Date
    Dec 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi again,thanks for replying. i added the alert to my setcookie function and nothing happened...

    heres my external file:

    [code]

    /* Code from chapter 7 */
    /* Beginning of Cookie Code Based on code by Bill Dortch
    of hidaho designs who has generously placed it in the public domain.*/
    function SetCookie(name,value,expires,path,domain,secure){
    var temp = name + "=" + escape(value);
    if (expires){
    temp += "; expires=" + expires.toGMTString();
    }
    if (path){
    temp += "; path=" + path;
    }
    if (domain){
    temp += "; domain=" + domain;
    }
    if (secure){
    temp += "; secure";
    }
    document.cookie = temp;
    }
    function GetCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i,j) == arg){
    return getCookieVal(j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
    }
    function getCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1){
    endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset,endstr));
    }
    function DeleteCookie (name,path,domain) {
    if (GetCookie(name)) {
    var temp = name + "=";
    temp += ((path) ? "; path=" + path : "");
    temp += ((domain) ? "; domain=" + domain : "");
    temp += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    document.cookie = temp;
    }
    }
    /* End of Cookie Code */

    /* converts days to milliseconds */
    function daysToMS(days){
    return days * 24 * 60 * 60 * 1000;
    }

    /* converts weeks to milliseconds */
    function weeksToMS(weeks){
    return weeks * 7 * 24 * 60 * 60 * 1000;
    }

    /* converts years to milliseconds */
    function yearsToMS(years){
    return years * 365.25 * 24 * 60 * 60 * 1000;
    }
    /* End of code from chapter 7 */

    [code]





    im not getting any errors either? the "store score" button does nothing


    thank you!

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    /* Code from chapter 7 */
    /* Beginning of Cookie Code Based on code by Bill Dortch
       of hidaho designs who has generously placed it in the public domain.*/
    function SetCookie(name,value,expires,path,domain,secure){
    	var temp = name + "=" + escape(value);
    	if (expires){
    		temp += "; expires=" + expires.toGMTString();
    	}
    	if (path){
    		temp += "; path=" + path;
    	}
    	if (domain){
    		temp += "; domain=" + domain;
    	}
    	if (secure){
    		temp += "; secure";
    	}
    	document.cookie = temp;
    }
    function GetCookie(name){
    	var arg = name + "=";
    	var alen = arg.length;
    	var clen = document.cookie.length;
    	var i = 0;
    	while (i < clen) {
    		var j = i + alen;
    		if (document.cookie.substring(i,j) == arg){
    			return getCookieVal(j);
    		}
    		i = document.cookie.indexOf(" ", i) + 1;
    		if (i == 0) break;
    	}
    	return null;
    }
    function getCookieVal(offset){
    	var endstr = document.cookie.indexOf(";", offset);
    	if (endstr == -1){
    		endstr = document.cookie.length;
    	}
    	return unescape(document.cookie.substring(offset,endstr));
    }
    function DeleteCookie (name,path,domain) {
      if (GetCookie(name)) {
        var temp = name + "=";
        temp += ((path) ? "; path=" + path : "");
        temp += ((domain) ? "; domain=" + domain : "");
        temp += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        document.cookie = temp;
      }
    }
    /* End of Cookie Code */
    
    /* converts days to milliseconds */
    function daysToMS(days){
    	return days * 24 * 60 * 60 * 1000;
    }
    
    /* converts weeks to milliseconds */
    function weeksToMS(weeks){
    	return weeks * 7 * 24 * 60 * 60 * 1000;
    }
    
    /* converts years to milliseconds */
    function yearsToMS(years){
    	return years * 365.25 * 24 * 60 * 60 * 1000;
    }
    /* End of code from chapter 7 */
    This is how you should use code tags
    Anyways, if you don't get an alert as suspected, somewhere along the lines there is an error.

    If you don't have it, i suggest you download Firefox (it's a good browser anyways), and try your page in that. Then look at the javascript console. It should tell you where errors occured, giving you the line number of the error code to look at.

    NOTE: I think i see the error. you have this:
    Code:
    <input type = "button" value = "Store Score" = onclick="submitCookie()">
    You forgot the semicolon.

  • #5
    New Coder
    Join Date
    Dec 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got it thank u so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •