Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2012, 09:28 PM   PM User | #1
fineartist99
New Coder

 
Join Date: Jul 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
fineartist99 is an unknown quantity at this point
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;
    }
};
fineartist99 is offline   Reply With Quote
Old 09-19-2012, 10:07 PM   PM User | #2
Sammy12
Registered User

 
Join Date: Jun 2011
Posts: 1,063
Thanks: 12
Thanked 241 Times in 240 Posts
Sammy12 is on a distinguished road
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..
Sammy12 is offline   Reply With Quote
Old 09-20-2012, 02:30 PM   PM User | #3
fineartist99
New Coder

 
Join Date: Jul 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
fineartist99 is an unknown quantity at this point
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 : )
fineartist99 is offline   Reply With Quote
Old 09-20-2012, 02:41 PM   PM User | #4
Sammy12
Registered User

 
Join Date: Jun 2011
Posts: 1,063
Thanks: 12
Thanked 241 Times in 240 Posts
Sammy12 is on a distinguished road
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..
Sammy12 is offline   Reply With Quote
Users who have thanked Sammy12 for this post:
fineartist99 (09-20-2012)
Old 09-20-2012, 02:43 PM   PM User | #5
fineartist99
New Coder

 
Join Date: Jul 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
fineartist99 is an unknown quantity at this point
so that would go into my first chunk of code right?
fineartist99 is offline   Reply With Quote
Old 09-20-2012, 07:32 PM   PM User | #6
fineartist99
New Coder

 
Join Date: Jul 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
fineartist99 is an unknown quantity at this point
Awesome, thank you. I see the expiration in chrome now. Thank you thank you.
fineartist99 is offline   Reply With Quote
Reply

Bookmarks

Tags
cookies, expiration date set

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:35 AM.


Advertisement
Log in to turn off these ads.