/    Sign up×
Community /Pin to ProfileBookmark

Clam Shell Menu

Hi everyone!

I’m new to javascript and I run into a problem here. I’m currently building a clam shell menu, one that shows/hides its sub-menu when it’s clicked. I’d like to maintain the state of this menu across different pages without using cookies. I’m not sure if there’s such a thing as global static variable in javascript but I tried. The code is shown as below:

[COLOR=red]
var sub_1 = 0;
var sub_2 = 0;

function toggleClamShellMenu(objectID) {
var object = document.getElementById( objectID );
if( object.style.display == ‘block’ ){
object.style.display = ‘none’;
if( objectID == ‘menu1’ ) sub_1 = 0;
if( objectID == ‘menu2’ ) sub_2 = 0;
}
else{
object.style.display = ‘block’;
if( objectID == ‘menu1’ ) sub_1 = 1;
if( objectID == ‘menu2’ ) sub_2 = 1;
}
return;
}

function initialShellMenu(){
var object = document.getElementById( ‘menu1’ );
if( sub_1 == 1 ) object.style.display = ‘block’;

object = document.getElementById( ‘menu2’ );
if( sub_2 == 1 ) object.style.display = ‘block’;

}
[/COLOR]

The body tag calls the [COLOR=red]initialShellMenu()[/COLOR] onload. The menu functions but is unable to maintain the state. I’m looking for some pointers.

Respect to all of you! Tks!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@ggriffitJan 04.2005 — The only way you can do it without Cookies is to use Frames - one frame holding the state - or to put a flag(s) in the QueryString of the browser. If your code is Server Side generated - e.g. PHP/Perl/ASP then you could use Sessions to store that data.
Copy linkTweet thisAlerts:
@eraserauthorJan 04.2005 — Tks ggriffit!

Using a frame seems to be a very bad practice nowadays. I try to avoid it entirely. Server side technology is not an option in this case.

If this isn't possibly done using javascript alone, could anyone tell me what actually fails in the code please? Is it javascript's inability to track the two variables or is it sth else?

Tks!
Copy linkTweet thisAlerts:
@ggriffitJan 04.2005 — The problem is that Javascript is stateless by itself, so it needs something else - e.g. Browser QueryString, cookie, a page that has not been refreshed/loaded with the values in etc - to hold its state values.
Copy linkTweet thisAlerts:
@eraserauthorJan 04.2005 — Tks once again!
×

Success!

Help @eraser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...