Home arrow JavaScript arrow Page 3 - Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully
JAVASCRIPT

Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully


The modern trend aimed at building highly interactive and visually appealing web sites can introduce undesirable effects, particularly in those cases where JavaScript is used erroneously to display relevant content to end users. Therefore, if you're interested in learning how to make your JavaScript applications degrade gracefully when scripting has been disabled in the client, then this series of articles might be what you're looking for.

Author Info:
By: Alejandro Gervasio
Rating: 3 stars3 stars3 stars3 stars3 stars / 4
June 27, 2007
TABLE OF CONTENTS:
  1. · Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully
  2. · Creating a simple navigation bar
  3. · An example: a fictional web page
  4. · Extending the implementation of the JavaScript-based navigation bar

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Making a Dynamic Navigation Bar in JavaScript Degrade Gracefully - An example: a fictional web page
(Page 3 of 4 )

All right, now that you've seen how the "index.php" file looks, let me go one step further and show you the signature of a fictional "About us" web page. Its corresponding definition is as follows:

(definition for about.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 - About us page</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>About us</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>

Naturally, as you might have guessed, the signature of the above PHP file is nearly identical to that of the "index.php" file that you learned previously, except that the respective navigation bar would look like this:

So far, so good right? At this point you hopefully learned how to create a simple JavaScript application for adding a neat dynamic touch to a text-based navigation bar, which can also degrade silently when scripting has been disabled on the browser.

So what is the next step? Well, simply in the section to come I'm going to show you the signatures corresponding to the rest of the web pages that comprise this fictional web site, so you can see in detail the dynamic behavior exposed by the respective navigation bar when a user visits the different pages.

Click on the link below and keep reading. 


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials