JavaScript Tutorial/Development/Cookie

Материал из Web эксперт
Версия от 18:52, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create a cookie and read it back

<HTML>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
<!--
function set_it() {
   var the_text="name=yourName&";
   var toexpire= new Date("March 15, 2008");
   var expdate="expires="+toexpire.toGMTString();
   the_text+=expdate;
   var newtext=escape(the_text);
   document.cookie=newtext;
}
function read_it() {
 if (document.cookie) {
    var mycookie=document.cookie;
    var fixed_cookie= unescape(mycookie);
    var thepairs= fixed_cookie.split("&");
    var pair1= thepairs[0];
    var pair2= thepairs[1];
    var namevalue1= pair1.split("=");
    alert("read");
    alert("Welcome, "+namevalue1+"!");
 } else  {
   alert("set");
   set_it();
 }
}
read_it();
//-->
</SCRIPT>
</BODY>
</HTML>


Delete all cookies

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
expireAt = new Date;
expireAt.setMonth(expireAt.getMonth() - 1);
if (document.cookie != "")
{
        crumbs = document.cookie.split(";");
        for(i=0; i < crumbs.length; i++)
        {
            crumbName = crumbs[i].split("=")[0];
            document.cookie = crumbName + "=;expires=" + expireAt.toGMTString();
        }
}
//  -->
</script>
</head>
<body>
</body>
</html>


JavaScript and Cookies

A cookie is a small bit of information stored in a text file on the user"s computer by a browser.

The cookie object is part of the Document object.

Cookies can be created, set, and modified by setting the appropriate values of the cookie property.

A cookie has four name attributes: expires, path, domain, and secure.

By default, a cookie lasts only during the current browsing session.

For a cookie to last beyond the current browsing session, the expires attribute must be set.

The value of expires attribute can be set to any valid date string.

The path attribute specifies the domain associated with the cookie.

The level of association begins at the specified path and goes down into any subfolders.

So for example, suppose http://www.wbex.ru/examples/cookie.html was setting a cookie and wanted the cookie to be shared across Web pages on the wbex.ru domain.

To do this, the cookie path attribute needs to be set to "/".

This allows the cookie to be accessed from any page on the www.wbex.ru Web server.

If the path was set to "/examples", the cookie would only be valid to pages in the examples folder and its subfolders.

If the secure attribute is specified, the cookie will be only be transmitted over a secure channel (HTTPS).

If secure is not specified, the cookie can be transmitted over any communications channel.

4. 4. Cookie 4. 4. 1. JavaScript and Cookies 4. 4. 2. <A href="/Tutorial/JavaScript/0080__Development/ReadingCookies.htm">Reading Cookies</a> 4. 4. 3. <A href="/Tutorial/JavaScript/0080__Development/WritingCookies.htm">Writing Cookies</a> 4. 4. 4. <A href="/Tutorial/JavaScript/0080__Development/Createacookieandreaditback.htm">Create a cookie and read it back</a> 4. 4. 5. <A href="/Tutorial/JavaScript/0080__Development/MakecookieusingJavascript.htm">Make cookie using Javascript</a> 4. 4. 6. <A href="/Tutorial/JavaScript/0080__Development/Deleteallcookies.htm">Delete all cookies</a> 4. 4. 7. <A href="/Tutorial/JavaScript/0080__Development/Readcookievalue.htm">Read cookie value</a>

Make cookie using Javascript

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
expireAt = new Date;
expireAt.setMonth(expireAt.getMonth() + 3);
username = "";
function makeCookie()
{
    username = document.form1.user1.value
    document.cookie = "name=" + username + ";expires=" + expireAt.toGMTString()
}
//  -->
</script>
</head>
<body>
<form name="form1">
<P>Username: <input type="text" name="user1"><br>
<a href="http://www.wbex.ru" onclick="makeCookie()">continue ...</a></p>
</form>
</body>
</html>


Read cookie value

<html>
<head>
<title></title>
<script language="JavaScript">
<!--
function fillIn()
{
    if (document.cookie != "")
    {
        cookieCrumb = document.cookie.split("=")[1];
        document.form1.read1.value = cookieCrumb;
    }
    else
    {
        document.form1.read1.value = "Cookie empty!";
    }
}
//  -->
</script>
</head>
<body onload="fillIn()">
<form name="form1">
<P>The username you entered was: <input type="text" name="read1"></p>
</form>
</body>
</html>


Reading Cookies

var cookieName = document.cookie;


Writing Cookies

Cookie values can be created, modified, and set.

When you write a cookie, its attributes must be written as a name/value pair.

To set a cookie value, you first create a cookie and assign it a name.

Then you must set each individual attribute for the new cookie.

The expires attribute must have a valid date string.



document.cookie = "name=" + form.cookie.value + "; expires=" + month;