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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Js set cookie expiration

    I've read multiple threads about this but none of them pertain to my code that I found and am using. My code from my js file is below, I would like the cookies to expire in 24 hours and I would think the code below would do that, but it doesn't. I just waited 24 hours after closing the browser window to see if the pop up occurred and it did not. Any help would be very helpful.

    Code:
    jQuery.cookie = function(name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            // NOTE Needed to parenthesize options.path and options.domain
            // in the following expressions, otherwise they evaluate to undefined
            // in the packed version for some reason...
            var path = options.path ? '; path=' + (options.path) : '';
            var domain = options.domain ? '; domain=' + (options.domain) : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    };

  • #2
    Registered User
    Join Date
    Jun 2011
    Posts
    1,063
    Thanks
    12
    Thanked 241 Times in 240 Posts
    Can we see what was the expire time you set? For 24 hours, the expire time should be "1."

    ------------------------

    You don't have to wait for the cookie to expire, you can see the expiration date in Chrome's inspect element.

    1. For Chrome, right click the page -> inspect element.



    2. Navigate to the "Resources" tab and click the website under "cookies"

    Last edited by Sammy12; 09-19-2012 at 10:18 PM.

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I did that and this is what I saw



    So no cookies are set according to that right? But that doesn't make sense because if you go to the site the pop up occurs, then if you navigate back to the homepage(the only page that the pop up happens on) the pop up doesn't occur - thus letting me know the cookies are actually working.

    Here's another js file that is associated with this page
    Code:
    //Launch lightbox upon load of document
    $(document).ready(function() {
    	
    	$('#sliderBox').stop().slideTo({
    		transition:1000,
    		left:'center',
    		inside:window
    	});
    
    	//Remember if user was shown lightbox
    	var cookieName = 'fam-offer';
    	if($.cookie(cookieName)!=cookieName){
    		$.cookie(cookieName, cookieName,{ path: '/' });	
    			
    		$('#sliderBox').slideTo({
    			transition:1000,
    			top:'center',
    			left:'center',
    			inside:window
    		});
    	}
    	
    	jQuery(window).resize(function() {
    		$('#sliderBox').stop().slideTo({
    			transition:300,
    			top:'center',
    			left:'center',
    			inside:window
    		});
    	});
    
    	var closeWelcomeBox;
    
    	$("#closebtn").click(function(){
    		$('#sliderBox').stop().slideTo({ 
    			transition:1000,
    			top:-2000
    	    	});
    	    	$('#background').fadeOut(500);
    		closeWelcomeBox = true;
    	});
    
    });
    
    
    //Hide the window
    function hideWindow(id){
    	$('#modal-mask').fadeOut(500);	
    	$('.modal-window').hide();
    }
    
    //Launch (animate) the window
    function launchWindow(id){
    	
    	//Remember if user was shown lightbox
    	var cookieName = 'swag-email';
    	if($.cookie(cookieName)!=cookieName){
    		$.cookie(cookieName, cookieName,{ path: '/' });
    	}	
    	
    	//Get the screen height and width
    	var maskHeight = $(document).height();
    	var maskWidth = $(window).width();
    
    	//Set height and width to mask to fill up the whole screen
    	$('#modal-mask').css({'width':maskWidth,'height':maskHeight});
    	
    	//transition effect		
    	$('#modal-mask').fadeIn(500);	
    	$('#modal-mask').fadeTo("slow",0.85);	
    
    	//Get the window height and width
    	var winH = $(window).height();
    	var winW = $(window).width();
    
    	//Dialog Dimensions
    	var dialogH = $(id).height();
    	var dialogW = $(id).width();
    		  
    	//Set the popup window to center
    	$(id).css('top',  winH/2-dialogH/2);
    	$(id).css('left', winW/2-dialogW/2);
    
    	//transition effect
    	$(id).fadeIn(1000); 
    }
    Maybe that would help anyone out with helping me : )

  • #4
    Registered User
    Join Date
    Jun 2011
    Posts
    1,063
    Thanks
    12
    Thanked 241 Times in 240 Posts
    So you want every 24 hours for the popup to re-appear?

    I don't see any expiration in your code:

    Code:
    if($.cookie(cookieName)!=cookieName){
    	$.cookie(cookieName, cookieName,{ path: '/' });	
    		
    	$('#sliderBox').slideTo({
    		transition:1000,
    		top:'center',
    		left:'center',
    		inside:window
    	});
    }
    There should be one set:

    Code:
    $.cookie(cookieName, cookieName, { path: '/', expires: 1 })
    On that same Chrome panel, you can right click a cookie and delete it to test things out again.

    @post below: yes. The second chunk is 'swag-email', I have no idea what that is.
    Last edited by Sammy12; 09-20-2012 at 02:46 PM.

  • Users who have thanked Sammy12 for this post:

    fineartist99 (09-20-2012)

  • #5
    New Coder
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    so that would go into my first chunk of code right?

  • #6
    New Coder
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Awesome, thank you. I see the expiration in chrome now. Thank you thank you.


  •  

    Tags for this Thread

    Posting Permissions

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