January 14, 2002 - Saving Cookies in Persistent Related Menus | WebReference

January 14, 2002 - Saving Cookies in Persistent Related Menus

Yehuda Shiran January 14, 2002
Saving Cookies in Persistent Related Menus
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

One of the problems of related menus is that once you go off to another Web page and then come back, the menu selections are not persistent. You can overcome this problem with cookies. The cookie property belongs to the document object. Play around with these related menus:

Write down the entries you see on both menus. Let's assume that the left and right menus are level2 and level23, respectively. Surf now to another Web site of your choice. Hit the browser's Back key. You will see that both menus persisted. The left menu will be level2 and the right menu will return to level23.

The trick is to save the menu index in a cookie whenever it changes, and restore the cookie upon the page's reloading. We save the cookie when the right menu ("list") changes:

function load(form, name) {
    var url = form[name].options[form[name].selectedIndex].value;
    setCookie("listSelectedIndex", document.forms[0].list.selectedIndex);
    return false;
  }
We save the right menu index only. We name our persistent variable as listSelectedIndex. Its value is document.forms[0].list.selectedIndex.

Tomorrow, we'll show you how to retreive this cookie and index.