JavaScript
  Home arrow JavaScript arrow Page 2 - Making a Dynamic Navigation Bar in JavaScr...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2007-06-27

    Table of Contents:
  • Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully
  • Creating a simple navigation bar
  • An example: a fictional web page
  • Extending the implementation of the JavaScript-based navigation bar

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully - Creating a simple navigation bar


    (Page 2 of 4 )

    According to the concepts deployed in the beginning, my intention here is to illustrate how the functionality of an existing element of a given web site can be improved via JavaScript, while still allowing that element to remain completely functional even if scripting has been disabled on the browser. 

    I'm going to create some basic PHP files which will display on top of the respective web page a conventional navigation bar. Then I'm going to attach to the mentioned web page a few simple JavaScript functions to turn the navigation bar into a dynamic element.

    Here is the signature for the PHP file that displays a conventional home page:

    (definition for index.php file)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example of dynamic navigation bar</title>
    <style type="text/css">
    body{
       padding: 0;
       margin: 0;
       background: #fff;
    }

    h1{
       font: bold 20px Arial, Helvetica, sans-serif;
       color: #000;
       text-align: center;
    }

    #navbar{
       width: 770px;
       padding: 10px;
       margin-left: auto;
       margin-right: auto;
       background: #9cf;
    }

    #navbar ul{
       list-style: none;
       padding: 0;
       margin: 0;
    }

    #navbar li{
       display: inline;
    }

    #navbar a,#navbar a:visited{
       padding: 10px;
       font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
       color: #000;
       text-decoration: none;
    }

    #navbar a:hover,#navbar a:active,#navbar a:focus{
       background: #fc0;
    }

    .activemenu{
       background: #fc0;
    }
    </style>
    </head>
    <body>
     
    <h1>Home</h1>
     
    <div id="navbar">
       
    <ul>
         
    <li><a href="index.php">Home</a></li>
         
    <li><a href="about.php">About us</a></li>
         
    <li><a href="products.php">Products</a></li>
         
    <li><a href="services.php">Services</a></li>
         
    <li><a href="contact.php">Contact</a></li>
       
    </ul>
     
    </div>
    </body>
    </html>

    As you can see, the above PHP file basically displays a primitive web page, which includes a text-based navigation bar on top of the respective web document. Of course, the look and feel of the navigation bar in question can be seen more clearly in the screen shot below:

    Logically, the navigation bar depicted above isn't anything special, since it doesn't differ too much from the ones that you've probably seen when surfing the web. Nevertheless, let me now show you the definition of a short JavaScript snippet that will be embedded into the web page that you saw before.

    Given that, this simple JavaScript code looks like this:

    <script language="javascript">
    function activateMenu(){
       var navbar=document.getElementById('navbar');
       if(!navbar){return};
       var links=navbar.getElementsByTagName('a');
       if(!links){return};
       for(var i=0;i<links.length;i++){
         if(links[i].getAttribute('href').indexOf
    (window.location.href.split('//')[1].split('/')[2])!=-1){          
           links[i].className='activemenu';
         }
       }
    }
    window.onload=function(){
       if(document.getElementById
         &&document.getElementsByTagName&&document.createTextNode){
         activateMenu();
       }
    }
    </script>

    As shown above, the previous JavaScript code is tasked simply with highlighting, on the navigation bar, the link that corresponds to the web page being currently visualized. Of course, there are many ways to achieve this effect with JavaScript, but I decided to use in this case, its popular "window.location" object.

    Now that you learned how the previous JavaScript snippet works, let me show you the new signature of the same "index.php" file listed previously, this time including the snippet in question:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example of dynamic navigation bar</title>
    <style type="text/css">
    body{
       padding: 0;
       margin: 0;
       background: #fff;
    }

    h1{
       font: bold 20px Arial, Helvetica, sans-serif;
       color: #000;
       text-align: center;
    }

    #navbar{
       width: 770px;
       padding: 10px;
       margin-left: auto;
       margin-right: auto;
       background: #9cf;
    }

    #navbar ul{
       list-style: none;
       padding: 0;
       margin: 0;
    }

    #navbar li{
       display: inline;
    }

    #navbar a,#navbar a:visited{
       padding: 10px;
       font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
       color: #000;
       text-decoration: none;
    }

    #navbar a:hover,#navbar a:active,#navbar a:focus{
       background: #fc0;
    }

    .activemenu{
       background: #fc0;
    }
    </style>
    <script language="javascript">
    function activateMenu(){
       var navbar=document.getElementById('navbar');
       if(!navbar){return};
       var links=navbar.getElementsByTagName('a');
       if(!links){return};
       for(var i=0;i<links.length;i++){
         if(links[i].getAttribute('href').indexOf
    (window.location.href.split('//')[1].split('/')[2])!=-1){          
           links[i].className='activemenu';
         }
       }
    }
    window.onload=function(){
       if(document.getElementById
          &&document.getElementsByTagName&&document.createTextNode){
         activateMenu();
       }
    }
    </script>
    </head>
    <body>
     
    <h1>Home</h1>
     
    <div id="navbar">
       
    <ul>
         
    <li><a href="index.php">Home</a></li>
         
    <li><a href="about.php">About us</a></li>
         
    <li><a href="products.php">Products</a></li>
          
    <li><a href="services.php">Services</a></li>
         
    <li><a href="contact.php">Contact</a></li>
        
    </ul>
     
    </div>
    </body>
    </html>

    As you might have guessed, the above PHP file looks nearly the same after embedding into it the JavaScript code that you learned before. However, now the functionality of the corresponding navigation bar has been improved, since it's now capable of highlighting the link of the page that's being currently viewed.

    This condition is clearly reflected by the screen shot below:

    As you can see, now the navigation bar has a slightly more dynamic behavior. Nonetheless, the most important thing to note here with reference to the functionality exposed by the navigational bar in question is that it remains nearly the same, regardless of whether the JavaScript snippet that you saw before is working.

    So, are you starting to grasp the concept behind building JavaScript applications that degrade gracefully? I bet you are!

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the course of this second tutorial of the series, a simple dynamic navigation ...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek