How to store an accordion menu's state (open or closed) using Javascript or PHP
I would imagine it would be done with a cookie or session, but what is the best way to remember an accordion menu's state if a user opens an item and leaves? How can I have "open" items in that state when the user comes back to the site?
I've just upgraded my custom CMS with a new interface, and the new menu needs something like this or usability 开发者_JAVA百科will suffer greatly.
I don't need specifics, just how you would normally do it.
Thanks!
http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/
this helped me out, hopefully it does for you as well.
I think you would need to save it to a database or use local storage (which isn't supported by all browsers). Cookies would work, but they can be deleted by the user (so can local storage). Sessions wouldn't work because once the user closes their browser, they lose their sessions.
Saving to a database or flat file is the most bulletproof way to do it.
Like most things, it depends.
If this is something that will make a server side request, then storing it in the session global would be completely acceptable.
If it's not, then you would need to find some other method of storing it client side. These would include things like storing it in javascript (only available the page's life), a cookie (like you mentioned) or like Bryan Downing mentioned with local storage. Local storage doesn't have good browser support yet.
Do you want it to be remembered regardless of the computer they log in from? If you do then set a simple value in a table associated to that user as to which item is open so it can be open the next time they return.
I use the jquery UI accordion and it's easy to update a field to which option was last selected so the next time the user comes back I can have that remembered for them. I've done this with a simple ajax call to request the value from the DB and return it to my javascript as a variable that's then used to set the opened accordion section.
If you're not worried about remembering across different devices then a simple cookie should do the trick, just updated the cookie each time a selection is made.
精彩评论