iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 8, 2008
Meet The Hardy Heron: What's New in Ubuntu 8.04
OReilly Network
 
May 8, 2008
Does Enterprise Development Have to Be Painful? (Part Two)
OReilly Network
 
May 8, 2008
Perl Pragma Primer
WebReference.com
 
May 8, 2008
1 comment
.net
 
May 7, 2008
Poll: Do you check the download speed of the pages you build?
About
 
May 6, 2008
Web Design Firms - 18 New Listings
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /JavaScript

Cookies with JavaScript 

  Views:    90577
  Votes:    1
by Ahmad Permessur 10/08/06 Rating: 

Synopsis:

In this tutorial we will be able to handle cookies using JavaScript. We will implement both script and HTML page.
Pages: firstback1 2 forwardlast
The Article

Example

It is time to join the script and the web page code. Insert the script in the HEAD tag. You do not need to type any line, the example is available to download.

This example saves the value of a form field in a cookie called DEVpapers.com and each time the user loads the document, a pop up alert appears with the value of the cookie. It will happen until the expiration date.

The function added is

function devpapersRead()
{
 var visited=devpapersOpenCookie("DEVpapers.com");

 if (visited!=null)
  alert(visited);
}
 

Then, we insert the following HTML code to invoke the JavaScript functions. As we said, every time the document is loaded (handled with the onLoad event), the function devpapersRead is called, and it alerts the user with the value of the cookie, if set. And each time users submit the form, the cookie is updated with new values.

<body bgcolor="#FFFFFF" onLoad="devpapersRead()">
<form action="" name="cookieForm" onSubmit="devpapersSave(document.cookieForm.cookieField.value)">
  <input type="text" name="cookieField">
  <input type="submit" name="cookieButton" value="Cookie">
</form>
 

Conclusion

Using JavaScript, we can handle useful information to, for example, personalise web sites. We made this example trivial for educational purposes –even we alert the value of the cookie-, but we can handle more useful data.

Pages: firstback1 2 forwardlast

Similar/related articles:


 
  Sponsors