Javascript cookie removal does not work sequentially

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!

0
source share
1 answer

I think that the deleted cookie will be disabled only after you go from the current page.

Paste the code below on the right after deleting the cookie;

location.href = "bringmeback.html";

bringmeback.html (redirect to the current page);

 location.href="currentpage.html"; 

You can use this code to check if your cookie is completely deleted.

0
source

Source: https://habr.com/ru/post/891757/


All Articles