|
January 15, 2002 Restoring Cookies in Persistent Related Menus Tips: January 2002
Yehuda Shiran, Ph.D.
|
|
One of the problems of related menus
is that once you visit another Web page and then come back, the menu selections
tend to change. 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. Now, surf to another Web site of your choice. Hit the browser's
Back
key. You will see that both menus have retained the entries of .level2
for the left menu and level23
for the right menu.
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 (
We restore the cookie when the page loads. Actually, we need to do more than that. The selected index of the left menu persists by itself, so we need to adjust again the right menu accordingly. If the left menu is
Let's see how we reflect this business logic in our code. First, we add the
The refreshOnDocLoad() function just calls the relate() function with the first form of the page:
The relate() function first initialized the options of the right menu to null:
Then, it assigns the relevant options from the left menu:
Now, it reads the cookie:
We just need to make sure the cookie is not empty. If it is, we select the first option. If it is not empty, we use its contents to pick the selected index:
People who read this tip also read these tips: Look for similar tips by subject: |