C
Chris Martin
Background: Our main menu is built using a simple unordered list
containing other ULs. There is a table on a page that holds the subnav.
Because of certain circumstances, I need to build the subnav dynamically
on each page. I wrote this script in about 15 minutes because it was a
"scope-creep" kinda thing. Now that I have a bit of time, for education
reasons, I want to optimize this thing as much as possible.
Notes: DOM is a heaven send
. By my own admission, the variable names
could be a little more descriptive. "nav" is the master navigation.
"subNav" is a table row which holds the sub-navigation. This script works
good enough. But, I'm always interested in the opinions of those more
experienced than I.
Here's a shortened version of the main nav:
<ul id="nav">
<li>
<div>
<a href="/home/newstory.aspx" alt="">Home</a>
</div>
</li>
<li>
<div>
<a href="/about/default.aspx" alt="ABOUT ASBA">About Asba
</a>
</div>
<ul>
<li>
<a href="/about/missionvisionphilosophy.aspx">
Mission/Vision/Philosophy</a>
</li>
<li>
<a href="/about/board.aspx">Board of Directors</a>
</li>
</ul>
</li>
</ul>
Here's the subnav:
<table class="subnav" border="0">
<tr>
<td class="subparent" id="subNavHeader">Benefits</td>
<td width="15" rowspan="2"> </td>
</tr>
<tr>
<td class="subbody" id="subNav">
</td>
</tr>
</table>
Here's the script:
function makeSubNav(){
var navs = document.getElementById('nav').getElementsByTagName('a');
var subNav = document.getElementById('subNav');
var path = formatPath( document.location.pathname );
var anchor, link, parentNodeName, listToPass, altText;
for(var i = 0; i < navs.length; i++){
anchor = navs;
link = formatPath( anchor.pathname );
listToPass = null;
if(path == link){
switch(anchor.parentNode.nodeName.toUpperCase()){
case "LI":
listToPass =
anchor.parentNode.parentNode.parentNode;
break;
case "DIV":
listToPass = anchor.parentNode.parentNode;
break;
}
break;
}
}
try{
altText = listToPass.getElementsByTagName('div')
[0].getElementsByTagName('a')[0].attributes.getNamedItem('alt').value;
}catch(err){
altText = '';
}
subNav.appendChild( createList(listToPass, altText, path) );
}
function formatPath(path){
if(path.charAt(0) != '/') path = '/' + path;
return path.toLowerCase();
}
function createList(ul, altText, currentPage){
document.getElementById('subNavHeader').innerHTML = altText;
var toReturn = document.createElement('ul');
var items = ul.getElementsByTagName('li');
var img, existingLink, existingPath, newItem, newLink, span;
for(var i = 0; i < items.length; i++){
existingLink = items.getElementsByTagName('a')[0];
existingPath = formatPath( existingLink.pathname );
newItem = document.createElement('li');
img = new Image();
img.height = 6;
img.width = 10;
img.border = 0;
img.src = "/Common/Images/LinkBullet.gif";
newItem.appendChild( img );
if(existingPath == currentPage){
toReturn.appendChild( document.createElement('br') );
newItem.className = "subcurrent";
span = document.createElement('span');
span.innerHTML = existingLink.innerHTML;
newItem.appendChild( span );
toReturn.appendChild( newItem );
toReturn.appendChild( document.createElement('br') );
}else{
newLink = document.createElement('a');
newLink.href = existingLink.href;
newLink.innerHTML = existingLink.innerHTML;
newItem.appendChild( newLink );
toReturn.appendChild( newItem );
}
}
return toReturn;
}
TIA
--
( )
)\ ) ) ( /(
(()/( ( /( ( ( ( )\())
/(_)))\()))\ ( )\))( ((_)\
(_)) (_))/((_) )\ ) ((_))\ _ ((_)
/ __|| |_ (_) _(_/( (()(_)| |/ /
\__ \| _| | || ' \))/ _` | | ' <
|___/ \__| |_||_||_| \__, | |_|\_\
|___/
containing other ULs. There is a table on a page that holds the subnav.
Because of certain circumstances, I need to build the subnav dynamically
on each page. I wrote this script in about 15 minutes because it was a
"scope-creep" kinda thing. Now that I have a bit of time, for education
reasons, I want to optimize this thing as much as possible.
Notes: DOM is a heaven send
could be a little more descriptive. "nav" is the master navigation.
"subNav" is a table row which holds the sub-navigation. This script works
good enough. But, I'm always interested in the opinions of those more
experienced than I.
Here's a shortened version of the main nav:
<ul id="nav">
<li>
<div>
<a href="/home/newstory.aspx" alt="">Home</a>
</div>
</li>
<li>
<div>
<a href="/about/default.aspx" alt="ABOUT ASBA">About Asba
</a>
</div>
<ul>
<li>
<a href="/about/missionvisionphilosophy.aspx">
Mission/Vision/Philosophy</a>
</li>
<li>
<a href="/about/board.aspx">Board of Directors</a>
</li>
</ul>
</li>
</ul>
Here's the subnav:
<table class="subnav" border="0">
<tr>
<td class="subparent" id="subNavHeader">Benefits</td>
<td width="15" rowspan="2"> </td>
</tr>
<tr>
<td class="subbody" id="subNav">
</td>
</tr>
</table>
Here's the script:
function makeSubNav(){
var navs = document.getElementById('nav').getElementsByTagName('a');
var subNav = document.getElementById('subNav');
var path = formatPath( document.location.pathname );
var anchor, link, parentNodeName, listToPass, altText;
for(var i = 0; i < navs.length; i++){
anchor = navs;
link = formatPath( anchor.pathname );
listToPass = null;
if(path == link){
switch(anchor.parentNode.nodeName.toUpperCase()){
case "LI":
listToPass =
anchor.parentNode.parentNode.parentNode;
break;
case "DIV":
listToPass = anchor.parentNode.parentNode;
break;
}
break;
}
}
try{
altText = listToPass.getElementsByTagName('div')
[0].getElementsByTagName('a')[0].attributes.getNamedItem('alt').value;
}catch(err){
altText = '';
}
subNav.appendChild( createList(listToPass, altText, path) );
}
function formatPath(path){
if(path.charAt(0) != '/') path = '/' + path;
return path.toLowerCase();
}
function createList(ul, altText, currentPage){
document.getElementById('subNavHeader').innerHTML = altText;
var toReturn = document.createElement('ul');
var items = ul.getElementsByTagName('li');
var img, existingLink, existingPath, newItem, newLink, span;
for(var i = 0; i < items.length; i++){
existingLink = items.getElementsByTagName('a')[0];
existingPath = formatPath( existingLink.pathname );
newItem = document.createElement('li');
img = new Image();
img.height = 6;
img.width = 10;
img.border = 0;
img.src = "/Common/Images/LinkBullet.gif";
newItem.appendChild( img );
if(existingPath == currentPage){
toReturn.appendChild( document.createElement('br') );
newItem.className = "subcurrent";
span = document.createElement('span');
span.innerHTML = existingLink.innerHTML;
newItem.appendChild( span );
toReturn.appendChild( newItem );
toReturn.appendChild( document.createElement('br') );
}else{
newLink = document.createElement('a');
newLink.href = existingLink.href;
newLink.innerHTML = existingLink.innerHTML;
newItem.appendChild( newLink );
toReturn.appendChild( newItem );
}
}
return toReturn;
}
TIA
--
( )
)\ ) ) ( /(
(()/( ( /( ( ( ( )\())
/(_)))\()))\ ( )\))( ((_)\
(_)) (_))/((_) )\ ) ((_))\ _ ((_)
/ __|| |_ (_) _(_/( (()(_)| |/ /
\__ \| _| | || ' \))/ _` | | ' <
|___/ \__| |_||_||_| \__, | |_|\_\
|___/