views:

452

answers:

2

I'm looking for some Javascript jedi-master to assist in creating a function that adds a class to a list of elements based on a session variable.

In other words:

If SESSION['MM_UserGroup'] is not equal to 1, then add the class '.hide-content' to the following elements:

.control #nav li#nav-admin .control #nav li#nav-pages

or any element I want to add to list.

The idea is to basically hide various navigational links if the person logged-in doesnt' have the correct access level.

Thanks very much for your feedback! Happy Friday!

+1  A: 

You're hiding items on a website... in client side javascript... based upon a session value? What's to stop me from looking to where the items point to and going there anyways?

Kolten
Its a fair question but how would you know where to point if there is nothing to indicate that there is anything to point to?
Jason Sweet
"View source" would let you see the complete HTML, including the URLs that you're not supposed to be able to access.
aem
I would use the force... but thats just me...
Zoidberg
+3  A: 

You can't, at least not with JavaScript alone.

Session information is stored on the server. Your question suggests you are using PHP sessions.

While you could use PHP to write data that JavaScript could read, and then use that to dynamically add an HTML class (there is no such thing as a CSS class) that would match a CSS selector to apply a rule to hide some data…

… it would be much simpler to just use PHP to determine if the links should be added to the document (or not) in the first place.

David Dorward
Thanks very much for your answer. Yes, using PHP sessions. So how would I ask the same question but taking Javascript out of the equation
Jason Sweet
Just wrap an if ($_SESSION['MM_UserGroup'] == 1) around the links you only want to appear for those users.
David Dorward