SoFunction
Updated on 2025-04-13

Very good [JS] Cookie Proficiency Path Page 2/2


// The constructor settings of Date() are in milliseconds
// .getTime()  method returns time, unit in milliseconds
// So to set the expiration of 15 minutes, it takes 60,000 milliseconds to 15 minutes
var expiration = new Date((new Date()).getTime() + 15 * 60000);
 = "username=" + escape()+ "; expires ="
+ () + "; path=" + "/" + "; _
domain=" + "" + "; secure";

Reading cookies values ​​is a bit like a trick, because you get all cookies belonging to the current document at once.

// The following statement reads all cookies belonging to the current document
var allcookies = ;

Now, we have to parse the different cookies in the allcookies variable and find the specified cookie of interest. This work is simple because we can use the extension provided by the Javascript language
Expand string support.

If we are interested in the cookie "username" assigned earlier, we can use the following script to read its value.

// We define a function to read a specific cookie value.
function getCookie(cookie_name)

var allcookies = ;
var cookie_pos = (cookie_name);

// If the index is found, it means that the cookie exists.
// On the contrary, it means that it does not exist.
if (cookie_pos != -1)

// Please cookie_pos at the beginning of the value and just add 1 to the value.
cookie_pos += cookie_name.length + 1;
var cookie_end = (";", cookie_pos);

if (cookie_end == -1)

cookie_end = ;


var value = unescape((cookie_pos, cookie_end));


return value;


// Call the function
var cookie_val = getCookie("username");

The cookie_val variable in the above routine can be used to generate dynamic content or sent to a server-side CGI script for processing. Now you know how to use Javascript script manipulation
Basic methods of cookies. But if you are like me, the first thing we have to do is to create some interface functions to hide the troubles of cookies processing. but,
Before you start programming, wait for a moment. Someone has done these tasks for you long ago. All you have to do is just go where to find these interface functions.

For example, in David Flangan's Javascript: The Definitive Guide 3rd Ed., you can find good cookie application classes. You can also use Orelly's WEB site
Click to find the examples in this book. In the last link list of this article, there are some direct links to access these cookie examples.



Cookies Monster

Cookies have a bad reputation for some reasons. Many people use cookies to do despicable things, such as traffic analysis, click tracking. Cookies are not very safe, especially if not
Cookies with secure attribute. However, even if you use secure cookies, if you share a computer with others, such as in an Internet cafe, then others can peek at the computer's hard drive without adding it
Secretly saved cookie files may steal your sensitive information. So, if you are a WEB developer, then you have to think about these issues seriously. Don't abuse cookies. No
Save data that users may consider to be sensitive in cookies. If you save the user's social insurance number, credit card number, etc. in the cookie, it is equivalent to placing these sensitive information under the window paper
, tantamount to investing users in great danger. A good principle is that if you don't want strangers to know about this information, don't save them in cookies.

In addition, there are some practical restrictions on cookies. Cookies are retained on the computer and do not follow the user. If the user wants to change the computer, the new computer cannot get the original cookie.
Even if users use different browsers on the same computer, they cannot get the original cookies: Netscape cannot read Internet Explorer cookies.

Also, users are unwilling to accept cookies. So don't think that all browsers can accept cookies you send. If your browser does not accept cookies, you must ensure your WEB site
The point will not collapse or interrupt.

In addition, the WEB browser may not retain more than 300 cookies. There are no standards for when and how the browser will invalidate cookies. Therefore, when the limit is reached, the browser can have
Effectively delete cookies randomly. The browser retains no more than 20 cookies from one WEB server, and the data (including name and value) of each cookie is not more than 4K bytes. (
However, the cookie size in this article is fine. It only takes up 12 K bytes and is saved in 3 3 cookies. )

In short, be careful to keep cookies simple. Don't rely on the existence of cookies, don't save too much information in each cookie. Don't save too many cookies. However, to eliminate these restrictions
, In the hands of highly skilled WEB administrators, the concept of cookies is a useful tool.

External links
Every Javascript programmer should have a Javascript: David Flanagan’s The Definitive Guide.  In this book, I found cookie routines to help you
More than one variable encodes to a single cookie, overcoming the "limit of 20 cookies per WEB server". Please click this link to download the routine.
ftp:///pub/examples/nutshell/javascript/。
Previous page12Read the full text