Javascript Cookie Function not working for Domain
Posted
by danit
on Stack Overflow
See other posts from Stack Overflow
or by danit
Published on 2010-05-07T13:20:10Z
Indexed on
2010/05/07
14:18 UTC
Read the original article
Hit count: 241
Here are the functions Im using:
Set Cookie:
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
var cookie_string = name + "=" + escape ( value );
if ( exp_y )
{
var expires = new Date ( exp_y, exp_m, exp_d );
cookie_string += "; expires=" + expires.toGMTString();
}
if ( path )
cookie_string += "; path=" + escape ( path );
if ( domain )
cookie_string += "; domain=" + escape ( domain );
if ( secure )
cookie_string += "; secure";
document.cookie = cookie_string;
}
Read Cookie:
function get_cookie ( cookie_name )
{
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
if ( results )
return ( unescape ( results[2] ) );
else
return null;
}
Delete Cookie:
function delete_cookie ( cookie_name )
{
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
The Jquery I use to construct the cookie:
if(get_cookie('visible')== 'no') {
$("#wrapper").hide();
$(".help").hide();
$("#slid .show").show();
$("#slid .hide").hide();
} else {
$("#slid .show").hide();
$("#slid .hide").show();
}
$("#slider").click(function() {
if(get_cookie('visible')== null) {
set_cookie('visible','no', 2020, 01,01, '/', 'domain.com');
} else {
delete_cookie('visible');
}
$(".help").slideToggle();
$("#wrapper").animate({ opacity: 1.0 },200).slideToggle(200, function() {
$("#slid img").toggle();
});
});
Im trying to set the cookie for all pages that exist under domain.com with the path '/'.
However using these functions and jQuery it doesn't appear to be working, any anyone give me an idea of where im going wrong?
© Stack Overflow or respective owner