1

I have a login form at the bottom of the screen that toggles open and close with a slidetoggle. How could I have it so, If the user changes pages the open or close state stays the same? Ajax update panels maybe? Here is my JQuery code I have for the toggle:

       <script type="text/javascript">
          if ($('#divLoginFootForm').is(":visible")) {
              $('#divLoginFootForm').show();

          }
          else {
              $('#divLoginFootForm').show();

          }

          $('.log').click(function () {
              $('#divLoginFootForm').slideToggle('slow', function () {
                  // Animation complete

              });
          });



</script> 

Thanks!

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
Det
  • 39
  • 1
  • 7
  • Do you mean it remembers the toggled state when they leave and come back to that page? – MattW Dec 15 '11 at 15:24
  • Indeed or navigate to other pages within the site. I am using a masterpage. – Det Dec 15 '11 at 15:38
  • Did any of the answers below help? You can save the visible value to a cookie (like mentioned below) then use .toggle(bool) to set the initial toggle to the one stored in the cookie. Check out http://api.jquery.com/toggle/ – MattW Dec 15 '11 at 16:57
  • I havent attempted yet. I have a feeling the cookie solution should work in theory. I created cookies before. I shall let you know. – Det Dec 15 '11 at 23:55

4 Answers4

1

You can use cookie to maintain the state. Take a look at this examples

http://www.electrictoolbox.com/jquery-cookies/

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
0

Store the visible state of the form to a cookie. Upon subsequent requests, read the visible state from the cookie and apply it to the form.

You can easily incorporate the three functions from this page on quirksmode for reading/writing/deleting cookies.

karim79
  • 339,989
  • 67
  • 413
  • 406
0

Track the state of the toggle in a hidden field, and then set the toggle state when the page initially loads. You need to use a hidden field since the value has to be accessible on both the client and server. One the client side, when the user toggles the state, set the val of the hidden field to reflect the state.

Then, since you're using asp.net, the state will be preserved between postbacks, and you can control the initial state, etc. in the code-behind.

If you use a cookie, then the state will be shared between all instances of the page a user may open (i.e. in multiple tabs), which may not be what you want.

Edit: Here's a question which illustrates how to set the value of a hidden field with jquery.

Community
  • 1
  • 1
chris
  • 36,094
  • 53
  • 157
  • 237
  • How would I grab the state and return it into that field? I could also use a global variable to do the same thing. – Det Dec 15 '11 at 15:40
  • A global var in the page isn't going to be accessible to the client. – chris Dec 15 '11 at 16:10
  • Yes, but it's not going to be updated when the user toggles the visibility since that only happens on the client. – chris Dec 15 '11 at 16:20
0

You have a couple options:

  1. Cookie

  2. Session Items Collection (if you are using ASP.Net)

Cookie may be beneficial because it is not specific to your web platform, and jQuery could do it on its own.

Session Items Collection would be good if you wanted to keep it in code and not write it out to a cookie.

If there is no reason for the server to know about this state, I would suggest cookie.

one.beat.consumer
  • 9,414
  • 11
  • 55
  • 98
  • I am using ASP.Net. Could you elaborate on session items collection? – Det Dec 15 '11 at 23:56
  • `HttpContext.Current.Session[keyStr]` is a simple items collection of HttpSessionState. I use it to supplement cookies. It's super easy to use and understand - http://msdn.microsoft.com/en-us/library/system.web.httpcontext.session.aspx - is quick documentation to get you started. Again, if only your client-side script needs to know then I would suggest a cookie like others. – one.beat.consumer Dec 16 '11 at 17:43