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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Help with session cookie for Lightbox

    I need a session cookie that way only my lightbox only pops up one time per visit. I have no idea how to do this. Can anyone help? I've searched for session cookies and they only confuse me.

  • #2
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Let me add, if someone will write a session cookie for me, I will pay $25 for it. Anyone?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Here you are:-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function setCookie(isName,isVal) {
    document.cookie = isName + "=" + isVal + ";expires="  + "path=/";
    }
    
    function getCookie(isName){
    cookieStr = document.cookie;
    startSlice = cookieStr.indexOf(isName+"=");
    if (startSlice == -1) {return false}
    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;
    }
    
    function dispCookie(isName) {
    nValue = getCookie(isName);
    if (nValue) {
    alert ("Cookie Found");  // so do nothing
    }
    
    else {
    alert ("The Cookie was not found");
    
    //so add the code hereto cause Lightbox to appear
    
    }
    }
    
    </script>
    
    </head>
    
    <body>
    <input type = "button" value = 'Set Cookie' onclick = "setCookie('myCookie','myCookieValue')">
    <br><br>
    <input type = "button" value = 'Read Cookie' onclick = "dispCookie('myCookie')">
    
    </body>
    </html>
    Please send your $25 to a medical charity of your choice.


    "The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Awesome. Thanks so much!

    A couple of questions:

    1. Do I change anything? For example, "IsName" do I add something there?
    2. Is there a site that teaches about cookies. I self-taught HTML and CSS, and am currently learning java script. But cookies itself confuse the heck out of me.

    Thanks again. I will donate $25 to the American Cancer Society.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by javanewbie7 View Post
    Awesome. Thanks so much!

    A couple of questions:

    1. Do I change anything? For example, "IsName" do I add something there?

    NO! All you have to do is add the code to invoke Lightbox at the point I indicated, that is if the cookie is not found. Obviously you will want to remove the alerts (just for testing), remove the buttons (again just for testing) and change the body tag to:-

    Code:
    <body onload = "dispCookie('myCookie'); setCookie('myCookie','myCookieValue')">
    In other words, onload first test to see if the cookie exists, if no cookie is found load Lightbox, if cookie is found do nothing, and then write a new cookie (whether or not it already exists).

    The name of the cookie is the literal string myCookie and its value is the literal string myCookieValue. You can change those if you want of course, but I see no reason to do so.

    IsName is not the same as isName. Javascript is case sensitive.

    Quote Originally Posted by javanewbie7 View Post
    2. Is there a site that teaches about cookies. I self-taught HTML and CSS, and am currently learning java script. But cookies itself confuse the heck out of me.
    Try w3schools.

    Quote Originally Posted by javanewbie7 View Post
    Thanks again. I will donate $25 to the American Cancer Society.
    Thank you for that!
    Last edited by Philip M; 11-16-2010 at 07:12 PM.

  • #6
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts
    One final question. (and please excuse my ignorance as I really don't know anything about cookies), why do I need buttons that say "set cookie" and "read cookie". Are they necessary? Thanks

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by javanewbie7 View Post
    One final question. (and please excuse my ignorance as I really don't know anything about cookies), why do I need buttons that say "set cookie" and "read cookie". Are they necessary? Thanks
    You do not need the buttons. There are there purely for testing purposes and help you understand what is happening.

    I have already said in Post #5:-

    Obviously you will want to remove the alerts (just for testing), remove the buttons (again just for testing) and change the body tag to

    Code:
    <body onload = "dispCookie('myCookie'); setCookie('myCookie','myCookieValue')">
    In other words, once you are satisfied that it is working, delete the alerts, remove the buttons, and change the body tag so that the cookie is set and read automatically on load (and not with the buttons). OK?
    Last edited by Philip M; 11-17-2010 at 08:19 AM.

  • #8
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Yeah, sorry, I saw where you said to remove the alerts, but I skipped over the button part. Thanks so much for your help!

  • #9
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I follow this Post! I'm equally grateful. I big thanks to Philip M and Newbie for asking the question.


  •  

    Posting Permissions

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