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

    Cookie and Java Help Needed

    OK, so a couple of days ago, someone on this forum was kind enough to write me a session cookie that way a lightbox would only pop up once per session. I've spent the last couple of days trying to combine to two to get them to work(they work separately.)

    Here's the cookie script:

    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>


    Here's my lightbox code:


    Code:
    <script type="text/javascript" src="./javascript/jquery/fancybox/jquery.mousewheel-3.0.4.pack.js"></script><script type="text/javascript" src="./javascript/jquery/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
    <link media="screen" href="./themes/KAC/jquery.fancybox-1.3.4.css" type="text/css" rel="stylesheet" />            	  <script type="text/javascript">
      $(document).ready(function() {
    	$("a#onload").fancybox({
    		'frameHeight':	390,
    		'frameWidth':	640,
    		'hideOnContentClick': false,
    		'easingIn': 2000
    			}).trigger('click'); 
    		});
    	}
    }
    
    </script></script></p>
    <div style="display: none;" id="lightbox_content">
    <div id="data"><img height="333px" width="700px" src=images/filecabinet/folder50/lightbox.jpg" title="Lightbox" alt="Double Your Donation: November 17-December 17" /></a>
    <h1>Support your local station today!</h1>
    </div>
    </div>
    <p></p>
    <p><!-- PLEDGE LIGHTBOX CODE ENDS HERE --></p>
    Sorry that I just don't "get it". But any help would be much appreciated. Thanksf

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,997
    Thanks
    79
    Thanked 4,433 Times in 4,398 Posts
    For what it's worth, NONE of that is JAVA code. It's all JavaSCRIPT.

    Yes, you are posting in the right forum. But don't call it Java. It's not. That's an entirely different language.

    Untested, but I think this will do it:
    Code:
    <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 null;}
        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;
    }
    
    $(document).ready(function() {
        if ( getCookie('myCookie') != null ) return; // no light box!
        setCookie('myCookie', 'ANY VALUE'); // for next time
        $("a#onload").fancybox({
    		'frameHeight':	390,
    		'frameWidth':	640,
    		'hideOnContentClick': false,
    		'easingIn': 2000
    			}).trigger('click'); 
    		});
    	}
    }
    </script>
    And forget about all the other stuff related to cookies.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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