Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Cookie

How can i create a cookie by using javascript just for end of the browser session(ie,upto closing of current browser).My script is like follows;

function setCookie(c_name,c_value,c_expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+c_expiredays);
    document.cookie=c_name+ "=" +escape(c_value)+
    ((c_expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

setCookie('gs_cookie','firstme',1600000);

How much value i need to pass instead of 1600000. Please help....

like image 784
Aadi Avatar asked Apr 12 '10 10:04

Aadi


People also ask

What is a JavaScript cookie?

A cookie is an amount of information that persists between a server-side and a client-side. A web browser stores this information at the time of browsing. A cookie contains the information as a string generally in the form of a name-value pair separated by semi-colons.

What is a HTML cookie?

An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser. The browser may store the cookie and send it back to the same server with later requests.

How many types of cookies are there in JavaScript?

There are three main types of cookies: session, persistent and third-party.

Is JS cookie secure?

The secure attribute is always activated for secured cookies, so it is transmitted with encrypted connections, without any hassles and security issues. The httpOnly flag does not give cookie access to JavaScript or any non-HTTP methods. This is situated in the secure cookie header.


2 Answers

If you dont set any expire time, the cookie will be deleted upon closing of the browser:

setCookie('gs_cookie','firstme');
like image 120
lugte098 Avatar answered Sep 30 '22 09:09

lugte098


Setting c_expiredays to 0 (without quotes) should do the trick.

like image 44
Pekka Avatar answered Sep 30 '22 11:09

Pekka