Account services

Bad habits 6: Unnecessary use of Javascript

What is wrong with this code snippet?

  <script type="text/javascript">
  <!--

          function openPage(action) {
                  document.forms["f"].ACTION.value=action;
                  document.forms["f"].submit();
          }
  //-->
  </script>

  <form name="f" action="main" method="post">
          <input type="hidden" name="app" value="" />
          <input type="hidden" name="ACTION" value="" />
          <input type="hidden" name="ACL" value="" />
  </form>

  <ul>
   <li><a href="javascript:openPage('C')"
       title='Show Applications'>Applications</a></li>
  </ul>
  

The problems

  1. It doesn't work if the user has Javascript turned off.
  2. It doesn't change the url in the address bar in the browser. Therefore the user can't bookmark the "Applications" page.

The replacement

All the code you see above can be replaced with three simple lines:

  <ul>
   <li><a href="main?ACTION=C" title='Show Applications'>Applications</a></li>
  </ul>