I have been working on converting a previous asp:menu item to JavaScript. Here is the JavaScript code I have come up with:
function GetMainMenu() {
var html = '';
var finalHTML = '';
finalHTML += '<div class="dropdown" style="width:100%">';
finalHTML += '<ul class="nav navbar-nav navbar-left">';
finalHTML += '<li class="dropdown">';
finalHTML += '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" style="height:18px;color:#003366">';
finalHTML += 'Main Menu<span class="caret"></span></a>';
var MainMenu = null;
MainMenu = new ActiveXObject("Microsoft.XMLDOM");
MainMenu = readXml("/public/FHSMenu.xml");
finalHTML += buildList(MainMenu.getElementsByTagName("Menu"));
finalHTML += '</li>';
finalHTML += '</ul>';
finalHTML += '</div>';
alert(finalHTML);
document.getElementById("preMainMenu").innerHTML = finalHTML;
var x = null;
x = new ActiveXObject("Microsoft.XMLDOM");
var item;
function buildList(data) {
html += '<ul class="dropdown-menu" role="menu">';
x = data;
for (item = 0; item < x.length ; item++) {
var y = x[item].getElementsByTagName("Title")[0].childNodes[0].nodeValue;
var u = null;
try {
u = x[item].getElementsByTagName("URL")[0].childNodes[0].nodeValue;
} catch (e) {
u = '';
}
if (u != '') {
html += '<li><a href="' + u + '" style="font-size:12px;color:#003366" >' + y + '</a></li>';
} else {
html += '<li class="dropdown-submenu" role="menu" >';
html += '<a href="#" data-toggle="dropdown" style="font-size:12px;color:#003366" >' + y + '</a>';
html += '</li>';
//var SubMenu = x[item].getElementsByTagName("Menu");
//if (SubMenu != undefined && SubMenu.length > 0) {
// html += buildList(SubMenu); // Submenu found. Calling recursively same method (and wrapping it in a div)
//}
}
}
html += '</ul>';
return html;
}
}
I am facing challenges while trying to display the root menus and submenus recursively from an XML file that was originally created for use with ASP:MENU. Any guidance on achieving this drop down menu functionality in JavaScript would be greatly appreciated.
<?xml version="1.0" standalone="yes"?> <LeftNavMenu> <Menu>
<MenuId>63</MenuId>
<Title>Clinical Care</Title>
<URL />
<ZOrder>0</ZOrder>
<Menu>
<MenuId>1660</MenuId>
<ParentId>63</ParentId>
<Title>Ambulatory EHR</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>3649</MenuId>
<ParentId>63</ParentId>
<Title>Angel Eye</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>1610</MenuId>
<ParentId>63</ParentId>
<Title>Clarity</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu> </Menu> <Menu>
<MenuId>3</MenuId>
<Title>Corporate</Title>
<URL />
<ZOrder>0</ZOrder>
<Menu>
<MenuId>1487</MenuId>
<ParentId>3</ParentId>
<Title>Become an Organ Donor</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>1845</MenuId>
<ParentId>3</ParentId>
<Title>Brand Style Guide</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>2153</MenuId>
<ParentId>3</ParentId>
<Title>Calendars</Title>
<URL />
<ZOrder>0</ZOrder>
<Menu>
<MenuId>2155</MenuId>
<ParentId>2153</ParentId>
<Title>Freemanhealth.com Events</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>2391</MenuId>
<ParentId>2153</ParentId>
<Title>Kronos Calendar</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>2158</MenuId>
<ParentId>2153</ParentId>
<Title>Payroll</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>2156</MenuId>
<ParentId>2153</ParentId>
<Title>Professional Development</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>2157</MenuId>
<ParentId>2153</ParentId>
<Title>Room Scheduling</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
</Menu>
<Menu>
<MenuId>8</MenuId>
<ParentId>3</ParentId>
<Title>Car Pool</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>1280</MenuId>
<ParentId>3</ParentId>
<Title>CMN (Children's Miracle Network)</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu>
<Menu>
<MenuId>181</MenuId>
<ParentId>3</ParentId>
<Title>Employee Handbook</Title>
<URL>some_link</URL>
<ZOrder>0</ZOrder>
</Menu> </Menu> </LeftNavMenu>