I would like to delete the cookie named "username" once the user closes their tab/browser. I am using the following code to detect if user closes and cookie should be deleted. But cookie is still there. Why?
In browser console resources, it shows cookie expiry field as "session". When does a session end?
// delete cookies when user closes browser/tab
$(window).unload(function() {
$.removeCookie('username', { path: '/' });
});
To create cookie the js is:
To remove the same cookie the code is:
You missed the second parameter, the value, so in this case your cookie will expire at session end.
The library I used is:
This library is no more maintained. With the new plugin:
To create a cookie:
and to remove:
By default, if no expire date is specified, the Cookie is removed when the user closes the browser (session cookie).
Because a browser can be closed with alf+f4 or with the X or with a kill signal you can only handle and rely on the cookie expire time.