Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:15:44+00:00 2026-06-02T15:15:44+00:00

So my PHP webiste generates DHTML output that looks like the following: <div class=toggle-ctrl

  • 0

So my PHP webiste generates DHTML output that looks like the following:

<div class="toggle-ctrl" onclick="toggleMenu();">
    click me to toggle menu
</div>
<div id="site-menu">
    <ul>
        <li>opt 1</li>
        <li>opt 2</li>
    </ul>
</div>
<p><a href="#">Link to Myself</a></p>

And of course, when clicked, the first div calls some JavaScript which toggles the visibility of the site-menu

function toggleMenu() {
    var navigation_pane  = document.getElementById('site-menu').style;
    if ( navigation_pane.display == 'none' )
        navigation_pane.display = 'block';
    else
        navigation_pane.display = 'none';
}

All this works fine. It’s clicking on the link which is bothering me right now. Clicking it (of course) creates a new http request, and my PHP engine re-generates the page again.

The problem occurs when the visibility of the site-menu is 'none'. The PHP engine doesn’t know that the menu is hidden, so it generates the same-html again, and the browser places the menu back in front of the surprised-looking user.

The question therefore, is how do I inform PHP (or how can PHP go to check) what the status of the site-menu‘s visibility is, before it goes to re-generate the page?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:15:45+00:00Added an answer on June 2, 2026 at 3:15 pm

    There are at least two options other than sending the menu state to the PHP script.

    1. Use AJAX to load just part of the page. If you don’t reload the menu, you don’t need to re-initialize its style. Before going down this path, examine whether AJAX is suitable. If you implement this solution, don’t break browser functionality.
    2. Modern browsers support a storage mechanism. Store the menu state in localStorage when it changes, and set the menu state when the page loads. To support older browsers, you can create an API that uses web storage when available and cookies when not (jQuery.Storage does this).

      Menu.js:

      /* implementation of Storage, Class and addEventListenerTo left as 
         an exercise for the reader.
      */
      var Menu = {
          init: function(id, toggleId) {
              if (! toggleId) {
                  toggleId = id + '-toggle';
              }
              var toggler = document.getElementById(toggleId),
                  menu = document.getElementById(id);
              menu.toggler = toggler;
              /* addEventListenerTo should call the browser-supplied event subscriber 
                 method (e.g. addEventListener or attachEvent)
              */
              addEventListenerTo(toggler, 'click', 
                  function(evt) {
                      Menu.toggle(id);
                  });
      
              if (! Storage.exists(id+'-open')) {
                  Storage.set(id+'-open', true);
              }
      
              if (Storage.get(id+'-open')) {
                  Menu.open(id);
              } else {
                  Menu.close(id);
              }
          },
          toggle: function(id) {
              var menu = document.getElementById(id);
              Class.toggle(menu, 'open closed');
              if (Class.has(menu, 'open')) {
                  menu.toggler.firstChild.nodeValue = 'close menu';
                  Storage.set(id + '-open', true);
              } else {
                  menu.toggler.firstChild.nodeValue = 'open menu';
                  Storage.set(id + '-open', false);
              }
          },
          setState: function (id, toAdd, toRemove) {
              var menu = document.getElementById(id);
              Class.remove(menu, toRemove);
              Class.add(menu, toAdd);
          },
          open: function(id) {
              this.setState(id, 'open', 'closed');
          },
          close: function(id) {
              this.setState(id, 'closed', 'open');
          }
      };
      

      some CSS file:

      .closed { display: none; }
      

      page:

      <div id="site-menu-toggle" class="toggle-ctrl">close menu</div>
      <div id="site-menu" class="open">
          <ul>
              <li>opt 1</li>
              <li>opt 2</li>
          </ul>
      </div>
      <p><a href="#">Link to Myself</a></p>
      
      <script type="text/javascript">
      Menu.init('site-menu');
      </script>
      

      You can play with a live version of the Menu.js approach on jsFiddle. Using jQuery, you can do away with Menu.js, resulting in a much more succinct implementation:

      <script type="text/javascript">
      $('#site-menu-toggle').click(function (evt) {
              var $menu = $('#site-menu');
              $menu.toggleClass('open close');
              $.Storage.set('site-menu-state', $menu.attr('class'));
              if ($menu.hasClass('open')) {
                  $('#site-menu-toggle').text('close menu');
              } else {
                  $('#site-menu-toggle').text('open menu');
              }
          });
      $(function() {
          var state = $.Storage.get('site-menu-state');
          if (! state) {
              $.Storage.set('site-menu-state', $('#site-menu').attr('class'));
          } else {
              $('#site-menu').attr('class', state);
          }
      });
      </script>
      

      There’s a jFiddle for the jQuery menu state implementation that you can play with.

    Since differences in the menu state don’t conceptually make for different resources, it doesn’t matter whether having the menu open or closed is bookmarkable or affected by history.

    NB. don’t use the text “click me“, it’s too verbose and redundant (what other action is there? Affordances should be implicit.). Instead, you can use a graphic to indicate open/close, or simply say “open menu”/”close menu”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a PHP script that dynamically generates transparent PNGs for use as CSS
I have website that where the php code generates all the products on the
I'm creating a website that generates a text file. I would like for the
I am developing a video website ( PHP - MYSQL ), just like youtube,
I've been tasked to maintain a PHP website with a function which automatically generates
Is SEO affected by the type of titles PHP generates? Eg: CamelCased link (not
Basically I hate a satellite website that is including form.php from an entirely different
Basically I want to write php code that lists all the contents that are
I have been using the encryption class of codeigniter (PHP framework) for a while
I currently have a school assignment that involves both PHP and asp.net. Now the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.