简体   繁体   中英

Javascript cookie not getting deleted on expiry in angularjs application

I need to store a cookie with an expiry period as 4 minutes in an AngularJS application controller.

My code:

var d = new Date();
d.setTime(d.getTime() + ( 4 *60*1000));

var expDate = "expires="+ d.toUTCString();

document.cookie = "testVal =" + data + ";expires=" + expDate + ";this.hostName;path=/";

After the expiry period of 4 minutes, the cookie should get deleted by itself, but is not getting deleted and continues to persist.

But when I set the same cookie, using the same line of code, via the browser console, it does get deleted on expiry.

Browser: IE v11

Am I missing anything while writing Cookie? Any help or suggestion is appreciated.

 const createCookie = (name, value, minutes) => { const date = new Date(); date.setTime(date.getTime()+(minutes*60*1000)); const expires = minutes? "; expires="+date.toGMTString(): ""; document.cookie = `${name}=${value}${expires}; path=/`; } createCookie("CookieName", "Cookievalue", 5);

You can use this function to create a cookie for how many minutes you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM