• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. Page Navigation Techniques > 10.4. Passing Data Between Pages Via Cookies

Passing Data Between Pages Via Cookies

NN 2, IE 3

Problem

You want to move user-influenced data from one of your pages to another without server intervention using browser cookies.

Solution

Using the cookies.js library from Recipe 1.9, you can use the onunload event handler of one page to store from one to twenty name/value pairs on the user’s machine. The following example captures a text input field’s value and saves it to a cookie that stays on the visitor’s computer for 180 days:

<script language="JavaScript" type="text/javascript" src="cookies.js"></script>
<script language="JavaScript" type="text/javascript" >
function saveData( ) {
    var data = document.forms[0].userName.value;
    var expDate = getExpDate(180, 0, 0);
    setCookie("userName", data, expDate); 
}
</script>
...
<body onunload="saveData( )">

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint