remember menu state of the same page between reloads

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

remember menu state of the same page between reloads

Post by newmember »

i have a php page and menu which initialy looks like this:

item 1
item 2
item 3

this menu links to page itself (with different query string).
when you click on item 1, for example, it shows submenu for this item:

item 1
__subitem 1
__subitem 2
item 2
item 3

when you click again on item 1, submenu hides...

item 1
item 2
item 3

(this is done by setting style.display to "none" and to "block")

When page loads first time, all submenus are hidden...
I hide them when onload event for document occurs:
<body onload="init()">

I will describe my problem with this menu with example:

1. lets say i click on item 1
2. menu expands and now looks like:

item 1
__subitem 1
__subitem 2
item 2
item 3

3. now i click on subitem 2 and it links to $_SERVER['SCRIPT_NAME'].'?tr=1'

4. so browser loads the same document + some other data (according to query string)

the very annoying thing now is that menu is reset and looks again like:

item 1
item 2
item 3

but i want it to be at same state it was before clicking subitem 2:

item 1
__subitem 1
__subitem 2
item 2
item 3

What can i do to get rid of this annoyence...?
(i want to make sure all submenus are closed very first time page loads)

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

so dynamically write to the style of the div or whatever you are using for the sub menus, and tell the one being looking at, to stay visible. Make sure the javascript checks the value of style.display property correctly, so it flips to the correct new value.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

...and tell the one being looking at, to stay visible...
but it is meaningless(?) to track display style of items in menu..because when i click on link in menu.
browser reloades everything and executes code in onload handler and that the place where i reset display style of menu.
so i don't know if i can do anything to remember menu state between page transitions...
(maybe i don't understand what you're saing)
the problem i'm describing is fixed very easy with frames...i put menu in a frame and page loads to another frame..
so menu always preserves it's state while i browsing...
but i don't want to use frames...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your onload doesn't need to switch the states. you can preset them within the php script.

now, you should have stuff already there that displays slightly differently for disabled javascript of course.

Javascript can look at the query string, btw. So you can determine from that whether to hide a particular one or not..
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

you can preset them within the php script
i don't need to preset menu state because it is already done so in stylesheet.
in my style sheet i set display:block as default for submenus...and in js i change to display:none for all submenus...
so if someone disables js then all submenus will be already visible...
(so i use js in onload to hide all submenus IF client supports js)
Javascript can look at the query string, btw. So you can determine from that whether to hide a particular one or not
can you please give any simple example to clarify this (because frankly speaking i don't follow you on this one :? ...)

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[google]+javascript +querystring[/google] :roll:
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

ok
thanks
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

i did it with js + query string and i almost sutisfied...
the last thing that left is what happens when i browse to another page(from the page with menu)
in the page with menu there are two kinds of links:
1. these that reference to page itself
2. these that point outside page

i took care of the first by adding additional param to query string but i not sure what can i do about 2...
right now i'm trying to do something with location.search but not very successfuly:
viewtopic.php?t=26318

do you have any ideas what can be done? or maybe just leave it as it is?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

check that thread.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

yes i hear you...
menu opens and closes by means of js so menu won't work anyway if js is disabled:D
acctualy i would love to do/learn something new...
one of things that stops me of doing that with sessions is that i don't really understand how to use it...
i saw a LOT of examples but it is like explaing what is integral to musician by solving tripple integral :D

do you know any links where it is explained in depth?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sessions are quite simple, I'd just screw around with them. All you often need to start playing with them is a call to [php_man]session_start[/php_man]() before any text output happens, then just start setting variables in the $_SESSION superglobal, switch pages, and check the variables, they should still be there if you have session_start on the second page too. :) this may help you get started with them.

Code: Select all

<?php

session_start();
if(!isset($_SESSION['somevar'])) $_SESSION['somevar'] = 1;
else $_SESSION['somevar']++;

var_export($_SESSION);
?>
reload that page several times, you should see the number keep going up and up.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

can you tell me some common errors that people make when dealing with sessions?
is it worse to learn how to work with sessions?
Last edited by newmember on Mon Sep 27, 2004 3:04 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not putting session_start in the right place.. that's 9/10 of the threads we get with sessions. :)
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

last question:)
is it worse to learn?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

worse to learn than what? I think they're real easy, as long as you know that it sends a header call, and need to place it before any output.. :)
Post Reply