0

I build up a website for a friend, but now I'm facing an issue with my js/css based vertical and collapsible menu. Everytime I click a link in there, the new page gets loaded and the whole menu collapses again. I tried to find a way to actually "store" the information what is opended, but failed so far. I was even looking into the option of just having some kind of "trigger" on the subpages loaded to actually tell the menu what part should be "open"... Hopefully someone can help me on this one.

Just as a pre-warning I'm not really used to code in html/php/js, so please try to explain possible solutions in a way a newbie might understand :-)

My code so far:

Javascript:

    $(document).ready(function() {

    // Collapse everything but the first menu:
    $("#VerColMenu > li > a").not("").find("+ ul").slideUp(1);

    // Expand or collapse:
    $("#VerColMenu > li > a").click(function() {
        $(this).find("+ ul").slideToggle("fast");
    });
});

CSS:

    <!-- jq Menu -->

#VerColMenu, #VerColMenu ul
    {
        list-style : none;
        width : 15em;
    }
#VerColMenu a
    {
        color : #ffffff;
        display : block;
        font-weight : bold;
        padding : 0.5em 1em;
        text-decoration: none;
        border-left: 12px solid #711515;
        border-right: 1px solid #711515;
        border-bottom: 1px solid #711515;
        background: #000000; /* Old browsers */
        background: -moz-linear-gradient(left,  #000000 1%, #8f0222 56%, #a90329 100%); /* FF3.6+ */
        background: -webkit-gradient(linear, left top, right top, color-stop(1%,#000000), color-stop(56%,#8f0222), color-stop(100%,#a90329)); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(left,  #000000 1%,#8f0222 56%,#a90329 100%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(left,  #000000 1%,#8f0222 56%,#a90329 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(left,  #000000 1%,#8f0222 56%,#a90329 100%); /* IE10+ */
        background: linear-gradient(to right,  #000000 1%,#8f0222 56%,#a90329 100%); /* W3C */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#a90329',GradientType=1 ); /* IE6-9 */

    }
#VerColMenu ul a
    {
        color : #ffffff;
        font-weight : normal;
        text-decoration: underline;
        background: #a90329; /* Old browsers */
        background: -moz-linear-gradient(left,  #a90329 0%, #8f0222 44%, #000000 99%); /* FF3.6+ */
        background: -webkit-gradient(linear, left top, right top, color-stop(0%,#a90329), color-stop(44%,#8f0222), color-stop(99%,#000000)); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(left,  #a90329 0%,#8f0222 44%,#000000 99%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(left,  #a90329 0%,#8f0222 44%,#000000 99%); /* Opera 11.10+ */
        background: -ms-linear-gradient(left,  #a90329 0%,#8f0222 44%,#000000 99%); /* IE10+ */
        background: linear-gradient(to right,  #a90329 0%,#8f0222 44%,#000000 99%); /* W3C */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a90329', endColorstr='#000000',GradientType=1 ); /* IE6-9 */
    }
#VerColMenu ul a:hover
    {
        background : #ffffff;
        text-decoration: underline;
        color : #000000;
    } 

HTML/PHP on Index.php (part for the content block where the link inside the menu will show up):

<div class="content">
                <p>
                <?php
                    $page = $_GET['page'];
                    # !! add all pages to the array !!
                    $pages = array('welcome', 'delivery', 'contact', 'impress', 'about', 'terms', );
                    if (!empty($page)) {
                        if(in_array($page,$pages)) {
                            $page .= '.php';
                            include($page);
                        }
                        else {
                        echo 'Page not found. Return to
                        <a href="index.php">index</a>';
                        }
                    }
                    else {
                        include('welcome.php');
                    }
                ?>
                </p>
        </div>

submenu.php (called inside index.php):

<ul id="VerColMenu">
<li><a title="Click to open or close this section" href="#">Bekleidung</a>
    <ul>
        <li><a href="index.php?page=tshirts">T-Shirts</a></li>
        <li><a href="index.php?page=girlies">Girlie-Shirts</a></li>
        <li><a href="index.php?page=longs">Longsleeves</a></li>
        <li><a href="index.php?page=hoodies">Kapuzenpullover</a></li>
        <li><a href="index.php?page=hoodiejackets">Kapuzenjacken</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Bekleidungs-Accesoires</a>
    <ul>
        <li><a href="index.php?page=labels">Kleidungs- & Wasch-Etiketten</a></li>
        <li><a href="index.php?page=coverband">Abdeckband für Nähte</a></li>
        <li><a href="index.php?page=zipper_puller">Reißverschluß-Zughilfe</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Kopf-, Hals- und Armbedeckung</a>
    <ul>
        <li><a href="index.php?page=caps">Caps</a></li>
        <li><a href="index.php?page=beanies">Beanies</a></li>
        <li><a href="index.php?page=scarves">Schals</a></li>
        <li><a href="index.php?page=bandanas">Bandanas</a></li>
        <li><a href="index.php?page=sweatbands">Schweißbänder</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Einlassarmbänder</a>
    <ul>
        <li><a href="index.php?page=alu">Stoffarmbänder mit Alu-Plombe</a></li>
        <li><a href="index.php?page=plastic">Stoffarmbänder mit Kunststoffverschluss</a></li>
        <li><a href="index.php?page=control_pvc">Kunststoffbänder</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Aufnäher / Patches</a>
    <ul>
        <li><a href="index.php?page=wovenpatch">Gewebte Aufnäher</a></li>
        <li><a href="index.php?page=embroiderypatch">Gestickte Aufnäher</a></li>
        <li><a href="index.php?page=backpatch">Bedruckte Rückenaufnäher</a></li>
        <li><a href="index.php?page=embroiderykeychain">Gestickte / Gewebte Schlüsselanhänger</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Accesoires</a>
    <ul>
        <li><a href="index.php?page=dogtags">Dog Tags</a></li>
        <li><a href="index.php?page=lanyard">Schlüsselbänder / Lanyards</a></li>
        <li><a href="index.php?page=keychain">Schlüsselanhänger</a></li>
        <li><a href="index.php?page=wallet">Geldbörsen</a></li>
        <li><a href="index.php?page=billholder">Mappen mit Prägung</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Banner & Flaggen</a>
    <ul>
        <li><a href="index.php?page=backdrop">Backdrop-Banner</a></li>
        <li><a href="index.php?page=combanner">Werbebanner</a></li>
        <li><a href="index.php?page=posterflag">Poster Flag</a></li>
        <li><a href="index.php?page=beachflag">Beachflag / Segel</a></li>
        <li><a href="index.php?page=aframe">A-Frame / Dreieck</a></li>
        <li><a href="index.php?page=rollup">Roll Up</a></li>
        <li><a href="index.php?page=golfflag">Golf Fahne</a></li>
    </ul>
</li>
 <li><a title="Click to open or close this section" href="#">Giveaways</a>
    <ul>
        <li><a href="index.php?page=bannerpen">Banner-Kugelschreiber</a></li>
        <li><a href="index.php?page=teepeg">Golftees</a></li>
        <li><a href="index.php?page=reflarm">reflektierende Armbänder</a></li>
        <li><a href="index.php?page=reflector">reflektierende Anhänger</a></li>
        <li><a href="index.php?page=bags">Taschen</a></li>
        <li><a href="index.php?page=lanyard2">Schlüsselbänder / Lanyards</a></li>
        <li><a href="index.php?page=keychain2">Schlüsselanhänger</a></li>
    </ul>
</li>
 <li><a title="Click to open or close this section" href="#">Sicherheitsprodukte</a>
    <ul>
        <li><a href="index.php?page=reflarm2">reflektierende Armbänder</a></li>
        <li><a href="index.php?page=reflector2">Reflektoren</a></li>
        <li><a href="index.php?page=reflvest">Sicherheitswesten</a></li>
        <li><a href="index.php?page=refljack">Sicherheitsjacken</a></li>
        <li><a href="index.php?page=reflshirt">Sicherheits-T-Shirts</a></li>
    </ul>
</li>
<li><a title="Click to open or close this section" href="#">Anstecknadeln / Pins</a>
    <ul>
        <li><a href="index.php?page=button">Buttons</a></li>
        <li><a href="index.php?page=pin">Pins</a></li>
    </ul>
</li>

hunepa
  • 1
  • If you are building this website, I think a better approach is to build it in a way that full page including the menu is not reloaded with every navigation. Use AJAX or frames to only update the relevant part of the page. – marekful Nov 08 '13 at 10:22
  • 1
    @MarcellFülöp I can't believe you're suggesting the OP to use frames! – DontVoteMeDown Nov 08 '13 at 10:34
  • @DontVoteMeDown Haha, I surprised myself too :) but come'n technically, it's viable and it's still valid. Believe it or not, some sites even use it. – marekful Nov 08 '13 at 10:44
  • @MarcellFülöp Flash or Silverlight are technically viable too, some sites still use it, but do you would use it? – DontVoteMeDown Nov 08 '13 at 10:46

1 Answers1

1

For that cases I use localStorage to store a boolean. It's supported by all major browsers. I think it can work for you.

Your code maybe work like this:

$(document).ready(function() {

    // Collapse everything but the first menu:
    if (localStorage.getItem("menuopen") !== true) {
        $("#VerColMenu > li > a").not("").find("+ ul").slideUp(1);
    }

    // Expand or collapse:
    $("#VerColMenu > li > a").click(function() {
        var menu = true;

        if (localStorage.getItem("menuopen") === true) {
            menu = false;
        }

        localStorage.setItem("menuopen", menu);

        $(this).find("+ ul").slideToggle("fast");
    });
});

This solution avoids structure changes on your application like using Ajax or frames for content loading. I hope that helps.

Community
  • 1
  • 1
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • I don't think that only the open/closed state should be remembered in the OP's case but full tree state, i.e. what is open and what is closed an a multi-level tree navigation. – marekful Nov 08 '13 at 10:24
  • 1
    @MarcellFülöp yeah you may be right, but he can take some direction with this, now it's up to him. I shoudn't do all his job. – DontVoteMeDown Nov 08 '13 at 10:26
  • Thanks for this! I just tried to put that in, but for some reason I can't expand anything anymore on click. Did I missed something? Is "menuopen" coming from somewhere or is this predefined in jquery? – hunepa Nov 08 '13 at 11:08
  • @hunepa there's nothing to do with jQuery. "menuopen" is a key to store the boolean value on localStorage. Please read the [link](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage) I've related to understand better. If it is not working anymore than try to debug the process. The code I post is supossed to work, but not sure. – DontVoteMeDown Nov 08 '13 at 11:17
  • OK, found it. There was just a typo in the code (localStorage.gsetItem instead of localStorage.setItem). Now it opens up correclty. Anyway, its not keeping the expanded items when clicking a link. – hunepa Nov 08 '13 at 12:07
  • @hunepa Ops My Bad. But that it's up to you. You can store any key you want on the localStorage, so in addition to the `menuopen` key you may store the tree path and parse it to open/close exactly as you desire. – DontVoteMeDown Nov 08 '13 at 12:09