How would I add a 24 hours cookie session to this script?

Discussion in 'HTML & Website Design' started by se_p800, Apr 25, 2008.

  1. #1
    Ok total noob to html. I have this popup script on my forum:

    And I want to use a cookie so that is only popups up once every 24 hours. How would I do that? Thanks
     
    se_p800, Apr 25, 2008 IP
  2. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #2
    KnuTz, Apr 25, 2008 IP
  3. se_p800

    se_p800 Well-Known Member

    Messages:
    609
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    2
    #3
    Thanks that looks like what I need. For this popup I have, this if how I have it set up now:



    But, the popup still pops up every time the page is viewed. You can see the popup here, its not a typical popup: http://customizepsp.co.uk/forums/index.php?styleid=1

    BUT....If I change whats in red and bold to "amt" instead of "Modalbox.show" an actual popup box appears with an error in it, but only once! So Im halfway there. I just need to know why it doesnt work with "body onLoad="Modalbox.show('.." that I have :S

    Thanks
     
    se_p800, Apr 25, 2008 IP
  4. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #4
    okey i edited a bit the code save this as cookie.js or whatever you name it

    
    var exp = 1; // the number at the left reflects the number of days for the cookie to last
    // modify it according to your needs
    
    function newCookie(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 nameSG = 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(nameSG) == 0) return c.substring(nameSG.length,c.length); }
    return null; }
    
    function amt() {
    var num = readCookie('sancookie1')
    if (num < 1) {
    newCookie('sancookie1','testcookie1', exp);
     Modalbox.show('<form action=\'http://www.gamesites200.com/psp/in.php?id=1850\' method=\'post\' target=\'_blank\' \><img src=\'popup/vote.png\' width=\'500\' height=\'72\'><table width=\'100%\' border=\'0\' cellspacing=\'0\' cellpadding=\'5\'><tr><td align=\'center\' valign=\'top\'><strong>Please reward our efforts by voting for us. </strong><br> All votes are greatly appreciated. <br><br><br> <br></td></tr><tr><td align=\'right\' valign=\'bottom\'><input type=\'submit\' value=\'Yes, I want to vote!\' onclick=\'Modalbox.hide()\' /> <input type=\'button\' value=\'No..\' onclick=\'Modalbox.hide()\' /></td></tr></table></form>', {title: this.title,width: 500});
      }
    }
    
    Code (markup):

    add to your page
    <script type="text/javascript" src="popup/cookie.js"></script>

    //to use
    <body onload="amt();">
     
    KnuTz, Apr 25, 2008 IP
  5. se_p800

    se_p800 Well-Known Member

    Messages:
    609
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    2
    #5
    Thankyou for taking the time to help me.

    I created a new .js document with the code you just wrote, and added it to my popup directory.

    I now have this:

    And nothing shows up. Have I not done it right? Thanks

    EDIT: I changed where you wrote "amt" to "Modalbox.show" and the popup showed but still every time the page was viewed. I now changed it back ti amt and the popup doesn't show at all.
     
    se_p800, Apr 25, 2008 IP
  6. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #6
    the code that show the the popup is in the function amt(); so to call that
    
    function amt() {
    var num = readCookie('sancookie1')
    if (num < 1) {
    newCookie('sancookie1','testcookie1', exp);
     Modalbox.show('<form action=\'http://www.gamesites200.com/psp/in.php?id=1850\' method=\'post\' target=\'_blank\' \><img src=\'popup/vote.png\' width=\'500\' height=\'72\'><table width=\'100%\' border=\'0\' cellspacing=\'0\' cellpadding=\'5\'><tr><td align=\'center\' valign=\'top\'><strong>Please reward our efforts by voting for us. </strong><br> All votes are greatly appreciated. <br><br><br> <br></td></tr><tr><td align=\'right\' valign=\'bottom\'><input type=\'submit\' value=\'Yes, I want to vote!\' onclick=\'Modalbox.hide()\' /> <input type=\'button\' value=\'No..\' onclick=\'Modalbox.hide()\' /></td></tr></table></form>', {title: this.title,width: 500});
      }
    }
    
    HTML:
    use

    <body onload="amt();">

    tried it here in my PC and work perfectly
     
    KnuTz, Apr 25, 2008 IP
  7. se_p800

    se_p800 Well-Known Member

    Messages:
    609
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    2
    #7
    Ok I will try it again, thanks

    This is now the code I have:




    Does that look right to you?
     
    se_p800, Apr 25, 2008 IP
  8. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #8
    use this
     <body onload="amt();">
    HTML:
    instead of this :D

    <body onload="amt('<form action=\'http://www.gamesites200.com/psp/in.php?id=1850\' method=\'post\' target=\'_blank\' \><img src=\'http://customizepsp.co.uk/forums/popup/vote.png\' width=\'500\' height=\'72\'><table width=\'100%\' border=\'0\' cellspacing=\'0\' cellpadding=\'5\'><tr><td align=\'center\' valign=\'top\'><strong>Please reward our efforts by voting for us. </strong><br> All votes are greatly appreciated. <br><br><br> <br></td></tr><tr><td align=\'right\' valign=\'bottom\'><input type=\'submit\' value=\'Yes, I want to vote!\' onclick=\'Modalbox.hide()\' /> <input type=\'button\' value=\'No..\' onclick=\'Modalbox.hide()\' /></td></tr></table></form>', {title: this.title,width: 500}); return false;">
    HTML:
     
    KnuTz, Apr 25, 2008 IP
  9. se_p800

    se_p800 Well-Known Member

    Messages:
    609
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    2
    #9
    Ok so its now this that I have:



    Now does it look right?? :D
     
    se_p800, Apr 25, 2008 IP
  10. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #10
    yes it should work now
     
    KnuTz, Apr 25, 2008 IP
  11. se_p800

    se_p800 Well-Known Member

    Messages:
    609
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    2
    #11
    Hmm well when I visit http://customizepsp.co.uk/forums/index.php?styleid=1 which is where I have added that exact code it does not show up. Does it show for you? I will try on the PC downstairs..

    EDIT: Yes!! It worked :D I am so grateful. I have spent over a week trying to get this to work and thanks to you it is! Thankyou so much.
     
    se_p800, Apr 25, 2008 IP
  12. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #12
    no problem :rolleyes:
     
    KnuTz, Apr 25, 2008 IP
  13. se_p800

    se_p800 Well-Known Member

    Messages:
    609
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    2
    #13
    Ok another problem now, here's what I am currently trying to use:



    I have changed the code in red and now it is not working. Any ideas?
     
    se_p800, Jun 6, 2008 IP