top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to manage cookies in JQuery?

+3 votes
985 views

I want to delete cookies whenever user is navigating away from my page.

Cookies should be cleaned when user delete tab/browser or user change URL and press enter. It should not be deleted when user press 'F5' or click other link in same page.

posted Aug 30, 2013 by Satyabrata Mahapatra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Put more detail ie which language u are using I believe u want it for all browsers.
You are right. I want it for all the browsers.
Language can be Jquery or Javascript.

3 Answers

+1 vote

You can set a timeout of a certain number of days (10 here) on the cookie:

$.cookie("test", 1, { expires : 10 });

Here I want to stress one point If the expires option is omitted, then the cookie becomes a session cookie, and is deleted when the browser exits.

answer Aug 30, 2013 by Luv Kumar
Timeout will not work in my case.
I want to clear data as soon as user leave the page.
+1 vote

Try something like

<body onload="SetCookie()" onunload="del_cookie()">

where as

function del_cookie(name) {
    document.cookie = name +
    '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    }
answer Aug 30, 2013 by Salil Agrawal
But this same will clear the data if user try to refresh.
You can only detect when the page is unloaded (refresh or close), not when the window is closed that's what I know...
+1 vote

Can we check wheather it is a F5 press or not by this:

$('document').bind('keypress', function(e) {
            if (e.keyCode == 116){
                someCheckVal = true;
            }
        });
answer Sep 2, 2013 by Satyabrata Mahapatra
Should be yes, just test it and integrate with what Luv has suggested.
Similar Questions
+2 votes

• Cookie 1 Name: IPAddress
• Cookie 1 Value: the client's IP address
• Cookie 2 Name: VisitTime
• Cookie 2 Value: the timestamp where the

...