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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    My global vars cause problems :( Newer than newbie

    I am trying to pass values from one page to another using a cookie. I am writing a trivia game for couples. The female player enters her name and then presses a button to go to the male player screen.

    A cookie is written with the female player value and it all seems to work ok. But when the male player reads the cookie values into global vars something seems to go wrong when his cookie is getting written. I've debugged a bit but cannot figure out why the third alert in the writecookie does not show up.

    This is the script as it appears in my page html between the head tags. Note that readcookie fires on onload and writecookie works from a button click. Both scripts seem to activate, but the write one does not show the third alert.

    I am brand new to this and am really enjoying it. I have a lot of experience in homebrew and self taught Delphi but want to broaden my experience. I am loving adding effects to web pages and images using javascript. Wish I'd done it years ago.


    <script type="text/javascript">
    function readCookie()
    {
    if (document.cookie != "") //make sure the cookie exists
    {
    noName = document.cookie.split("=")[1];
    yourName = document.cookie.split("=")[2];
    yourName2 = document.cookie.split("=")[3];
    document.getElementById('female_player_name').innerHTML="<b>"+yourName2+"'s Personal Settings</b>";

    alert("Read Cookies : " + ">" + noName+ yourName+">"+yourName2);

    }
    else
    {
    var yourName2 = "Gillian NotFound";
    alert("Cookie not found!");

    document.getElementById('female_player_name').innerHTML="<b>"+yourName2+"'s Personal Settings</b>";
    }
    }
    function WriteCookie()
    {
    alert("Hello");
    alert("Hello2");
    alert(yourName+">"+yourName2);
    }
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Your function WriteCookie() does nothing except show some alerts.

    noName = document.cookie.split("=")[1];
    yourName = document.cookie.split("=")[2];
    yourName2 = document.cookie.split("=")[3];
    is not correct.

    var c = document.cookie.split("=");
    var noname = c[0]; // array index starts at 0
    var yourName = c[1];
    var yourName2 = c[2];

    But you should not use = as a delimiter. Use ~ or whatever.



    Cookies have been covered very many times in this forum - try using the search feature for working examples. That will probably save you a lot of time.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    joshuafalken (07-13-2012)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much. I am digesting this now and trying to figure out how to apply it.

    R
    Last edited by joshuafalken; 07-13-2012 at 02:11 PM.

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Only thing I am having trouble with now is how to make sure I am reading the same cookie I wrote. It seems to be reading an older one with a different (old) name?

    How do I specify the cookie to read:


    <script type="text/javascript">
    function readCookie()
    {
    if (document.cookie != "") //make sure the cookie exists
    {
    var c = document.cookie.split("~");
    var noname = c[0]; // array index starts at 0
    var yourName = c[1];
    var yourName2 = c[3];
    document.getElementById('female_player_name').innerHTML="<b>"+yourName2+"'s Personal Settings</b>";

    alert("Read Cookies : ><"+noname+ "><" + yourName+"><"+yourName2);

    }
    else
    {
    var yourName2 = "Gillian NotFound";
    alert("Cookie not found!");

    document.getElementById('female_player_name').innerHTML="<b>"+yourName1+"'s Personal Settings</b>";
    }
    }
    </script>

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    function getCookie(isName){
    cookieStr = document.cookie;
    startSlice = cookieStr.indexOf(isName+"=");
    if (startSlice == -1) {return false}  // the cookie was not found
    endSlice = cookieStr.indexOf(";",startSlice+1)
    if (endSlice == -1){endSlice = cookieStr.length}
    isData = cookieStr.substring(startSlice,endSlice);
    isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    return isValue;
    }
    Naturally you must specify the name of the cookie to be retrieved.
    Last edited by Philip M; 07-13-2012 at 05:23 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay I think I have asked enough dumb questions for now. I really appreciate your patience and thank you for your time.

    I'll be working furiously hard over the summer hols to get to grips with all of this.

    R

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by joshuafalken View Post
    Okay I think I have asked enough dumb questions for now. I really appreciate your patience and thank you for your time.

    I'll be working furiously hard over the summer hols to get to grips with all of this.

    R
    That is very gracious of you. We are all keen to help, but of course you must (as you recognise) study the topic as well. Good luck!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've been playing with the getcookie function and the only query I have so far is this:

    I read and then rewrite the cookie on each page (onload = getcookie and exit page = writecookie). Is the cookie overwritten or appended to? I want to create it from scratch each time but it seems to be getting appended.

    thanks

    R

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by joshuafalken View Post
    I've been playing with the getcookie function and the only query I have so far is this:

    I read and then rewrite the cookie on each page (onload = getcookie and exit page = writecookie). Is the cookie overwritten or appended to? I want to create it from scratch each time but it seems to be getting appended.

    thanks

    R
    Normally setting a cookie of the same name will over-write the previous one. But you do not show your writecookie() code.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm looking into using the Query String solution to pass the data from page to page now, as this seems more logical to me considering that I want to accumulate data that will be passed in by the users in 3 different pages. I was trying to rewrite the cookie after each page, but I found that I was getting old values.

    I'm still learning (will be for years now I guess) and realise that a server side script might also be what i need. But I would like to know why my cookie is not being changed when I write it in the exitting page and read it in the new page.

    Anyway, here's my write:

    <script>
    function WriteCookie()
    {
    cookievalue2= "spsettings="+escape(yourName)+"="+escape(yourName2)+";";

    document.cookie=cookievalue2;
    alert("Setting Cookies :>" + cookievalue2 );
    location="game_setup.html";
    }
    </script>
    Last edited by joshuafalken; 07-14-2012 at 04:42 PM.


  •  

    Posting Permissions

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