I have the following code to set, receive and delete cookies:
function set_cookie( name, value, expires, path, domain, secure ) { var today = new Date(); today.setTime( today.getTime() ); if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date( today.getTime() + (expires) ); document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" ); } function get_cookie( name ) { var start = document.cookie.indexOf( name + "=" ); var len = start + name.length + 1; if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return null; } if ( start == -1 ) return null; var end = document.cookie.indexOf( ";", len ); if ( end == -1 ) end = document.cookie.length; return unescape( document.cookie.substring( len, end ) ); } function delete_cookie(name) { document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; }
In my program, I have something called an active message, and use a cookie to remember the path and existence of the active message. Naturally, I have a message reset function that will delete the cookie. It looks like this:
function discard_message() { alert('cookie = '+get_cookie('active_message')); clear_active_message_cookie(); alert('should be null = '+get_cookie('active_message')); update_message('Discard', false, false); } function clear_active_message_cookie(){ delete_cookie("active_message"); }
As you can see, I turned on alerts to check if the cookie is read after it is deleted. The strange thing is that in one part of my application the cookie is successfully deleted, but discarding a message in another part of my application does not work. The second cookie warning displays the value of the cookie. I have confirmed that the cookie name is the same.
This is almost the same as my request to delete a cookie is refused. Does anyone know in what circumstances is this possible?
Thanks!
source share