JavaScript Tutorial/Development/Cookie

Материал из Web эксперт
Перейти к: навигация, поиск

Create a cookie and read it back

   <source lang="javascript">

<HTML> </HEAD> <BODY> <SCRIPT language="JavaScript">

</SCRIPT> </BODY> </HTML></source>


Delete all cookies

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


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

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> <form name="form1">

Username: <input type="text" name="user1">
<a href="http://www.wbex.ru" onclick="makeCookie()">continue ...</a>

</form> </body> </html></source>


Read cookie value

   <source lang="javascript">

<html> <head> <title></title> <script language="JavaScript">

</script> </head> <body onload="fillIn()"> <form name="form1">

The username you entered was: <input type="text" name="read1">

</form> </body> </html></source>


Reading Cookies

   <source lang="javascript">

var cookieName = document.cookie;</source>


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.



   <source lang="javascript">

document.cookie = "name=" + form.cookie.value + "; expires=" + month;</source>