473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

store menu array data between forms

My menu options provides hide/show checkbox areas on a form. During an IE postback or "back" the array is lost and the checkboxes disappear from view. Now I need a way to keep the array between pages so the added checkboxes on the form do not disappear.

Thanks for your suggestions.
Expand|Select|Wrap|Line Numbers
  1.       menu_status = new Array(); 
  2.       function showHide(theid){
  3.         if (document.getElementById) {
  4.            var switch_id = document.getElementById(theid);
  5.  
  6.         if(menu_status[theid] != 'show') {
  7.            switch_id.className = 'show';
  8.            menu_status[theid] = 'show';
  9.         }else{
  10.            switch_id.className = 'hide';
  11.            menu_status[theid] = 'hide';
  12.         }
  13.     }
  14. }
Expand|Select|Wrap|Line Numbers
  1.  <a class="a" href='#'>MainMenu<br>  
  2. <table>                        
  3.    <tr><td><a href="#" onclick="showHide('mymenu1')">jump1</a></td></tr>
  4.    <tr><td><a href="#" onclick="showHide('mymenu2')">jump2</a></td></tr>
  5.    <tr><td><a href="#" onclick="showHide('mymenu3')">jump3</a></td></tr>
  6.  
  7. </table>
  8.  
  9. <div id="mymenu1" class=hide>
  10. <input type="checkbox" name="Topic" value="_select1" /> jump1selection    
  11. </div>
  12.  
  13. <div id="mymenu2" class=hide>
  14. <input type="checkbox" name="Topic" value="_select2" /> jump2selection    
  15. </div>
  16.  
  17. <div id="mymenu3" class=hide>
  18. <input type="checkbox" name="Topic" value="_select3" /> jump3selection    
  19. </div>
  20.  
Aug 13 '07 #1
23 2213
acoder
16,027 Expert Mod 8TB
Use cookies to store the current state of the checkboxes.
Aug 13 '07 #2
Use cookies to store the current state of the checkboxes.
Thanks for the reply acoder.

I have been looking into adding a cookie to store the menu status but it's still unclear how to do this properly. If the user selects and deselects multiple menu options during a single session does storing that information in a cookie really make sense? If so would I set a time out on the cookie?

appreciate your feedback!
Aug 20 '07 #3
acoder
16,027 Expert Mod 8TB
Thanks for the reply acoder.

I have been looking into adding a cookie to store the menu status but it's still unclear how to do this properly. If the user selects and deselects multiple menu options during a single session does storing that information in a cookie really make sense? If so would I set a time out on the cookie?

appreciate your feedback!
You could do this just before leaving the page using onbeforeunload.
Aug 20 '07 #4
You could do this just before leaving the page using onbeforeunload.
ok, so far I have included <body onbeforeunload=getcookie(); >
added a document.cookie statement to show all menu options
and have created the below function but could still use some help.

Expand|Select|Wrap|Line Numbers
  1. //....snip...
  2.         if(menu_status[theid] != 'show') {
  3.            switch_id.className = 'show';
  4.            menu_status[theid] = 'show';
  5.            document.cookie = menu_status(theid);
  6. //.....snip....
  7.  
  8. function getcookie(menu_status) {
  9.         if (document.cookie.length > 0)
  10.         {
  11.         menu_status=document.cookie.indexOf(menu_status + " ")
  12.         if ( menu_status!= 1 )
  13.            {
  14.            menu_status=menu_startus + menu_status.length +1
  15.            }
  16.       }
  17. return " "
  18. }
  19.  
Aug 20 '07 #5
gits
5,390 Expert Mod 4TB
hi ...

i fixed the code tags for you ... please use it the right way:

Expand|Select|Wrap|Line Numbers
  1. [ code=javascript ] ... [ /code ] - javascript
  2. [ html ] ... [ /html ] - html
  3.  
kind regards
Aug 20 '07 #6
hi ...

i fixed the code tags for you ... please use it the right way:

Expand|Select|Wrap|Line Numbers
  1. [ code=javascript ] ... [ /code ] - javascript
  2. [ html ] ... [ /html ] - html
  3.  
kind regards

No problem. I'll be sure to use [code] comments on my future forum questions and replies. Thanks..

I continue to have difficulty creating a cookie on menu select. Can anyone show me the way so I can expand my knowledge of this area?

Much appreciated.
Aug 22 '07 #7
acoder
16,027 Expert Mod 8TB
I continue to have difficulty creating a cookie on menu select. Can anyone show me the way so I can expand my knowledge of this area?
Read about getting, setting and deleting cookies in this article and this tutorial.

If you use those functions, it will be easy to store the data.
Aug 22 '07 #8
Read about getting, setting and deleting cookies in this article and this tutorial.

If you use those functions, it will be easy to store the data.
Yes, w3schools.com is very informative. Thanks for the suggestions.

I am now able to store a cookie on the client machine and feel I am half way there. Since the menu can be selected multiple times before an actual submit I have decided the best place for the setCookie is on the submit. With this however, I have difficulty getting the menu_status into the cookie for redisplay.

Here is what I have tried.

Expand|Select|Wrap|Line Numbers
  1. menu_status = new Array(); 
  2.  
  3. function showHide(theid){
  4. if (document.getElementById) {
  5.   var switch_id = document.getElementById(theid);
  6.  
  7.         if(menu_status[theid] != 'show') {
  8.            switch_id.className = 'show';
  9.            menu_status[theid] = 'show';
  10.  
  11.           }else{
  12.            switch_id.className = 'hide';
  13.            menu_status[theid] = 'hide';
  14.           }
  15.    } 
  16. }
  17.  
  18. function setCookie(menuCookie,value,days) {
  19.       if (days) {
  20.       var date = new Date();
  21.       date.setTime(date.getTime()+(days*24*60*60*1000));
  22.       var expires = "; expires="+date.toGMTString();
  23.       }
  24.       else var expires = "";
  25.       document.cookie = menuCookie+"="+value+expires+"; path=/";
  26.       }
  27.  
  28. function getCookie(menuCookie) {
  29.     var nameEQ = menuCookie + "=";
  30.     var ca = document.cookie.split(';');
  31.      for(var i=0;i < ca.length;i++) {
  32.      var c = ca[i];
  33.      while (c.charAt(0)==' ') c = c.substring(1,c.length);
  34.       if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  35.  }
  36.  return null;
  37.  }    
  38.  
  39. function eraseCookie(menuCookie) {
  40.  createCookie(menuCookie,"",-1);
  41.  }
  42.  
[html]

<input type=submit value="Search" onclick="'validquery';setCookie('menuCookie','<%=r equest.getParameter("menu_status()")%>','7');" />

[/html]

This returns null in the cookie value.
Aug 22 '07 #9
acoder
16,027 Expert Mod 8TB
You can only use request.getParameter whilst the page is loading. After the page has loaded, to change anything, you will need Javascript. Did you mean to pass the array to the function? Try setting a cookie for each menu id. You could just loop over the array and set each one.
Aug 22 '07 #10
You can only use request.getParameter whilst the page is loading. After the page has loaded, to change anything, you will need Javascript. Did you mean to pass the array to the function? Try setting a cookie for each menu id. You could just loop over the array and set each one.
By setting the cookie on each menu selection doesn't that create a lot more traffic to the client machine? A client could select menu option 1, and 2 but then decide menu option 3 is what they really want before submitting. Thus creating three cookie updates for the single transaction. Can I pass the menu array data to a hidden field and either pass that between the forms or instruct the setCookie function to use the menu array data onsubmit?
Aug 23 '07 #11
Acoder, I could use some of that expertise of yours.

I have update my code to set menu status_id in a cookie.
The js also removes the cookie when the menu option is
selected the second time( which in this case hides the display
of that menu ). What I need help with is getting the cookie back
and displaying the appropriate menu according to the cookie value.

I'm stuck so anything you can suggestion would
be greatly appreciated.

thanks

Code: ( text )
Expand|Select|Wrap|Line Numbers
  1. function showHide(theid){    // theid is the menu option selected 
  2.  
  3.       if (document.getElementById) {
  4.       var switch_id = document.getElementById(theid);
  5.  
  6.        if(menu_status[theid] != 'show'){
  7.        switch_id.className = 'show';
  8.        menu_status[theid] = 'show';
  9.        setCookie('menuC',[theid],2);  //this works
  10.  
  11.      }else{
  12.            switch_id.className = 'hide'; 
  13.            menu_status[theid] = 'hide';
  14.            checkall('queryForm','Topic',false);
  15.            eraseCookie('menuC'); // distroy cookie
  16.           }
  17.     }
  18. }
  19.  
  20. function setCookie(menuC,value,days) {
  21.        if (days) { 
  22.        var date = new Date();
  23.        date.setTime(date.getTime()+(days*24*60*60*1000));
  24.     var expires = "; expires="+date.toGMTString();
  25.     }
  26.       else 
  27.        var expires = "";
  28.        document.cookie = menuC+"="+value+expires+"; path=/";
  29.     }
  30.  
  31. function getCookie(menuC) {
  32.     var nameEQ = menuC + "=";
  33.    var ca = document.cookie.split(';');
  34.    for(var i=0;i < ca.length;i++) {
  35.    var c = ca[i];
  36.    while (c.charAt(0)==' ') c = c.substring(1,c.length);
  37.    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);    
  38.  } 
  39.  
  40.  return null;
  41. }    
  42.  
  43. function eraseCookie(menuC) {
  44.    setCookie(menuC,"",-1);
  45. }
  46.  
  47.  
- javascript
Sep 17 '07 #12
acoder
16,027 Expert Mod 8TB
What I need help with is getting the cookie back and displaying the appropriate menu according to the cookie value.
I assume you would want this to happen when you revisit the page. Call a function which goes through each menu option and gets the cookie. If set, it shows the menu otherwise it is kept hidden.
Sep 18 '07 #13
I assume you would want this to happen when you revisit the page. Call a function which goes through each menu option and gets the cookie. If set, it shows the menu otherwise it is kept hidden.
Yes, upon pageback the menu option should still be visiable. I am able to get the cookie value back and alert it to the screen, however I am not able to find the correct syntax to unhide the div.class for the menu option selected.

Do you have any suggestions?

Code: (javascript)
Expand|Select|Wrap|Line Numbers
  1.  
  2. function menu_redisplay (queryForm) {
  3.     var x = getCookie('menuC')
  4.     switch (x){ 
  5.         case "menu1":
  6.             class = 'show'
  7.             break;
  8.         case "menu2":
  9.             class = 'show'
  10.             break;
  11.         case "menu3":
  12.             class = 'show'
  13.             break;
  14.         case "menu4":
  15.             class = 'show'
  16.             break;
  17.         default: 
  18.             alert("no menu in cookie")
  19.         }
  20. }
  21.  
- javascript
Sep 18 '07 #14
Yes, upon pageback the menu option should still be visiable. I am able to get the cookie value back and alert it to the screen, however I am not able to find the correct syntax to unhide the div.class for the menu option selected.

Do you have any suggestions?

Code: (javascript)
Expand|Select|Wrap|Line Numbers
  1.  
  2. function menu_redisplay (queryForm) {
  3.     var x = getCookie('menuC')
  4.     switch (x){ 
  5.         case "menu1":
  6.             class = 'show'
  7.             break;
  8.         case "menu2":
  9.             class = 'show'
  10.             break;
  11.         case "menu3":
  12.             class = 'show'
  13.             break;
  14.         case "menu4":
  15.             class = 'show'
  16.             break;
  17.         default: 
  18.             alert("no menu in cookie")
  19.         }
  20. }
  21.  
- javascript

Here is a menu cookie that works on single menu selections.
Next stage, build a cookie and js to support mulitple menu sections.


Code: (javascript)
Expand|Select|Wrap|Line Numbers
  1.  
  2.     function menu_redisplay (queryForm) {
  3.         var x = getCookie('menuC')
  4.         var switch_id = document.getElementById(x);
  5.         switch (x){ 
  6.         case "menu1":
  7.                   switch_id.className = 'show'
  8.         break;
  9.         case "menu2":
  10.                   switch_id.className = 'show'
  11.         break;
  12.         case "menu3":
  13.                   switch_id.className = 'show'
  14.         break;
  15.         case "menu4":
  16.                   switch_id.className = 'show'
  17.         break;
  18.                                 default
  19.                    }
  20.        }
  21.  
- javascript[/quote]

Thanks.
Sep 18 '07 #15
acoder
16,027 Expert Mod 8TB
Here is a menu cookie that works on single menu selections.
Next stage, build a cookie and js to support mulitple menu sections.
Now what you could do is have a separate cookie for each menu section.

Let's say you have 4 menus: menu1 through to 4. Define a function which takes a menu number as a parameter and gets the cookie value and if defined, sets the value to 'show'. Now loop over the values and call the function on each menu.
Sep 19 '07 #16
Associating a cookie to each of the menus is something I agree with except that there are 19 menu options and the max cookie to domain is 20 I believe. As there are other cookies being set within the application I might need to find a different solution.

I played around with the below subcookie code found on http://www.whatstyle.net/articles/28/subcookies site but have problems with the integration. The menu_status array is only storing the current selected menu option.

Code: (javascript)

Expand|Select|Wrap|Line Numbers
  1. /* circumventing browser restrictions on the number of cookies one can use */
  2. var subcookiejar = {
  3.     nameValueSeparator: '$$:$$',
  4.     subcookieSeparator: '$$/$$',
  5.     /* set a cookie. subcookieObj is a collection of cookies to be. Every member of subcookieObj is the name of the cookie, its value
  6.      * the cookie value
  7.      */
  8.     bake: function(menuC,subcookieObj,days,path){
  9.         var cookieValue = '';
  10.         for (var i in subcookieObj)
  11.         {
  12.             cookieValue += i + subcookiejar.nameValueSeparator;
  13.             cookieValue += subcookieObj[i];
  14.             cookieValue += subcookiejar.subcookieSeparator;
  15.         }
  16.         /* remove trailing subcookieSeparator */
  17.         cookieValue = cookieValue.substring(0,cookieValue.length-subcookiejar.subcookieSeparator.length);
  18.         cookiejar.bake(menuC,cookieValue,days,path);
  19.     },
  20.     /* get a subcookie */
  21.     fetch: function(menuC,subcookieName){
  22.         var cookieValue = cookiejar.fetch(menuC);
  23.         var subcookies = cookieValue.split(subcookiejar.subcookieSeparator);
  24.         for (var i=0; i<subcookies.length; i++)
  25.         {
  26.             var sc = subcookies[i].split(subcookiejar.nameValueSeparator);
  27.             if (sc[0]==subcookieName) return sc[1];
  28.         }
  29.         return null;
  30.     },
  31.     /* delete a subcookie */
  32.     crumble: function(menuC,subcookieName,days,path)
  33.     {
  34.         var cookieValue = cookiejar.fetch(menuC);
  35.         var newCookieObj = {};
  36.         var subcookies = cookieValue.split(subcookiejar.subcookieSeparator);
  37.         for (var i=0; i<subcookies.length; i++)
  38.         {
  39.             var sc = subcookies[i].split(subcookiejar.nameValueSeparator);
  40.             if (sc[0]!=subcookieName) newCookieObj[sc[0]] = sc[1];
  41.         }
  42.         subcookiejar.bake(menuC,newCookieObj,days,path);
  43.     }
  44. };
-javascript
Sep 19 '07 #17
gits, what exactly am I doing wrong with my code tags? I'd like to stop. tks
Sep 19 '07 #18
gits
5,390 Expert Mod 4TB
hi ...

use:

[CODE=javascript] code goes here [/code]

kind regards
Sep 19 '07 #19
Now what you could do is have a separate cookie for each menu section.

Let's say you have 4 menus: menu1 through to 4. Define a function which takes a menu number as a parameter and gets the cookie value and if defined, sets the value to 'show'. Now loop over the values and call the function on each menu.
Appreciate your help- as always. thanks.
I believe this is what you are suggestion(below) to capture the menu options during selection and store it/them in a cookie. does this look right to you? It is not storing the cookie so the var menu_state section must be wrong.


Expand|Select|Wrap|Line Numbers
  1.     menu_status = new Array();
  2.  
  3.     function showHide(theid){  
  4.  
  5.         if (document.getElementById) {
  6.             var switch_id = document.getElementById(theid);
  7.  
  8.             if(menu_status[theid] != 'show') {
  9.                switch_id.className = 'show';
  10.                menu_status[theid] = 'show';
  11.  
  12.           }else{
  13.                switch_id.className = 'hide';
  14.                menu_status[theid] = 'hide';
  15.             }
  16.             var menu_state = '';
  17.             for (menu_name in menu_status) {
  18.                 menu_state += menu_name + ":" + menu_status[menu_name] + "|";
  19.             }
  20.             document.cookie="menuCookie=" + escape(menu_state) + ";";
  21.         }
  22.     }
  23.  
  24.  
  25. function getCookie() {
  26.             var dc = document.cookie;
  27.             var cookies = dc.split(';');    
  28.             for(var looped=0; looped < cookies.length; looped++) {
  29.                 if (cookies[looped].match('menuCookie')) {
  30.                     var cookie_value = cookies[looped];
  31.                 }
  32.             }
  33.             cookie_value = cookie_value.substring(13);
  34.             cookie_value = unescape(cookie_value);
  35.             var menu_values = cookie_value.split('|'); 
  36.             for(var loop=0; loop < menu_values.length; loop++){
  37.                 var sub_menu_values = menu_values[loop].split(':');
  38.                 if (sub_menu_values[0] == '') {continue;}
  39.                 var switch_menu = document.getElementById(sub_menu_values[0]);
  40.                 switch_menu.className = sub_menu_values[1];
  41.                 menu_status[sub_menu_values[0]] = sub_menu_values[1];
  42.             }
  43.         }
  44.  
  45.  
Sep 20 '07 #20
update
Hi,

The below code is working (not perfectly but working).
menuCookie is storing the menu option and the state ( show or hide).
getCookie is also working on page load to redisplay selected menu options. and for this case it's perfect for post backs.

The issue is when multiple menu options are selected, all cookie menu options are set to 'show'. However, when a single menu option is re-selected to hide the display the cookie is updating which set all the menu options to hide rather than just the one.

I need to correct this so only the single menu option is updated in the cookie.

Suggestions??

Expand|Select|Wrap|Line Numbers
  1.  menu_status = new Array();
  2.  
  3.     function showHide(theid){  
  4.  
  5.         if (document.getElementById) {
  6.             var switch_id = document.getElementById(theid);
  7.  
  8.             if(menu_status[theid] != 'show') {
  9.                switch_id.className = 'show';
  10.                menu_status[theid] = 'show';
  11.  
  12.           }else{
  13.                switch_id.className = 'hide';
  14.                menu_status[theid] = 'hide';
  15.             }
  16.  
  17.             var menu_state = '';
  18.             for (menu_name in menu_status) {
  19.                 menu_state += menu_name + ":" + menu_status[theid] + "|";
  20.             }
  21.             // document.cookie="menuCookie=" + escape(menu_state) + ";";
  22.             setCookie('menuCookie=',escape(menu_state),2);  
  23.  
  24.         }
  25.     }
  26.  
  27.  
  28.     function getCookie() {
  29.             var dc = document.cookie;
  30.             var cookies = dc.split(';');    
  31.             for(var looped=0; looped < cookies.length; looped++) {
  32.                 if (cookies[looped].match('menuCookie')) {
  33.                     var cookie_value = cookies[looped];
  34.                 }
  35.             }
  36.             cookie_value = cookie_value.substring(13);
  37.             cookie_value = unescape(cookie_value);
  38.             var menu_values = cookie_value.split('|'); 
  39.             for(var loop=0; loop < menu_values.length; loop++){
  40.                 var sub_menu_values = menu_values[loop].split(':');
  41.                 if (sub_menu_values[0] == '') {continue;}
  42.                 var switch_menu = document.getElementById(sub_menu_values[0]);
  43.                 switch_menu.className = sub_menu_values[1];
  44.                 menu_status[sub_menu_values[0]] = sub_menu_values[1];
  45.             }
  46.         }    
  47.  
  48.  
Sep 20 '07 #21
acoder
16,027 Expert Mod 8TB
Line 19 is the problem. You're setting everything to the same state by using theid instead of menu_name.

Change it to:
Expand|Select|Wrap|Line Numbers
  1. menu_state += menu_name + ":" + menu_status[menu_name] + "|";
Sep 21 '07 #22
Line 19 is the problem. You're setting everything to the same state by using theid instead of menu_name.

Change it to:
Expand|Select|Wrap|Line Numbers
  1. menu_state += menu_name + ":" + menu_status[menu_name] + "|";
You Rock aCoder!

and... you just made my day. correcting the above in my code fixed the problem.

Very much appreciated.
Sep 21 '07 #23
acoder
16,027 Expert Mod 8TB
You're welcome. Glad to hear you got it working.
Sep 21 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dr. zoidberg | last post by:
Hello, I'm creating simple menu. MySQL: +----+-----+----------------------+ | id | sid | Title | +----+-----+----------------------+ | 1 | 0 | Main Menu 1 | 2 | 0 | Main Menu 2 | ...
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
1
by: ajay | last post by:
I have following code for a slide menu but i twiked it to work for a single level menu. Open it in a Browser to get a clear picture. I have 2 Qs 1) How to make first entry as non-link. i.e i...
1
by: John Fung | last post by:
Dear Everybody, I have a double menu select javascript, could anyone know how to change it, so that the result will display in a frame name called "content", please? Here is the script....
2
by: Stuart Norris | last post by:
Dear Group, I am new to c# and windows form designer - coming from a Motif background. I am attempting to develop an application for a touch screen and I need to have a menu system with a...
11
by: Crirus | last post by:
I have a map in a game. I need to make a list with all visible objects for a player So, any building and any unit offer a range of visibility over the map. Now VB question: How can I store...
6
by: Nick | last post by:
Hi there, I'm running into slight confusion over a .NET menu "control", how comes it doesn't get listed in the forms controls array? I'm trying to enumerate through *every* control on any form,...
17
by: GS | last post by:
the main menu in the application seemed to disappeared all together until I click on an control and select mainmenu1 in designer. then the mainmenu1 displays where it should be but running it or...
13
by: PetterL | last post by:
I writing a program where I read menu items from a file. But I have problem when I click an menu item i want it to mark that one as checked but I cant access the menu object of that item to see...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.