1. Includes Menu Script and Stylesheet To be able to generate menu from HTML, you will need to include NlsMenu Pro extension library for HTML nlsmenuext_htm.js in addition to core libraries.
<html>
<head>
<title>Implementing NlsMenu Pro</title>
<link rel="StyleSheet" href="styles/basic-h.css" type="text/css">
<link rel="StyleSheet" href="styles/basic-v.css" type="text/css">
<script src="nlsmenu/lib/nlsmenu.js"></script>
<script src="nlsmenu/lib/nlsmenueffect.js"></script>
<script src="nlsmenu/lib/nlsmenuext_htm.js"></script>
</head>
</html>
Note that the basic-h.css is stylesheet for menubar and basic-v.css for all the submenus. All rules in basic-h.css have special prefix horz_. This prefix is defined for menubar in the following steps. 2. Creates Menu from HTML Structure
Starting version 3.0, NlsMenu Pro support loading menu from HTML as alternative to Javascript.
Loading menu from HTML enables you to specify menu structure in HTML UL/LI format. The main advantage over menu with Javascript
is the links in the HTML can be indexed by search engine.
<html>
<head>
<title>Implementing NlsMenu Pro</title>
<link rel="StyleSheet" href="styles/basic-h.css" type="text/css">
<link rel="StyleSheet" href="styles/basic-v.css" type="text/css">
<script src="nlsmenu/lib/nlsmenu.js"></script>
<script src="nlsmenu/lib/nlsmenueffect.js"></script>
<script src="nlsmenu/lib/nlsmenuext_htm.js"></script>
</head>
<body>
<div id="menudiv" style='display:none'>
<ul id="main">
<li><a href="http://www.mydomain.com" title="Home">Home</a></li>
<li><a href="http://www.mydomain.com" title="Products">Products</a>
<ul id="subProducts">
<li><a href="http://www.mydomain.com" title="Computers">Computers</a>
<ul id="subComputers">
<li><a href="http://www.mydomain.com" title="Servers">Servers</a>
<li><a href="http://www.mydomain.com" title="Workstation and Desktop">Workstation and Desktop</a>
<li><a href="http://www.mydomain.com" title="NOtebook">Notebook</a>
</ul>
</li>
<li><a href="http://www.mydomain.com" title="Accessories">Accessories</a></li>
<li><a href="http://www.mydomain.com" title="Others" target="_blank">Others</a></li>
</ul>
</li>
<li><a href="http://www.mydomain.com" title="Services">Services</a>
<ul id="subServices">
<li><a href="http://www.mydomain.com" title="Networking Solution">Networking Solution</a></li>
<li><a href="http://www.mydomain.com" title="Custom Software Development">Custom Software Development</a></li>
</ul>
</li>
<li><a href="http://www.mydomain.com" title="About Us">About Us</a></li>
</ul>
</div>
<script>
var menuMgr = new NlsMenuManager("MyMenu");
menuMgr.defaultEffect="fade";
//Create menu from HTML
menuMgr.loadMenuFromHTML("menudiv", true, false);
//Setup the menu
var menuBar = menuMgr.menubar;
menuBar.orient="H";
menuBar.stlprf="horz_";
//Render the menu
menuMgr.renderMenubar("menudiv");
</script>
</body>
</html>
As shown in example above, menu structure is defined in regular HTML tags such as UL/LI and A. Function used to create menu from HTML is loadMenuFromHTML(). This function takes 4 paremeters to determine how the menu will be generated:
Once the menu is created, you will be able to expose menubar and submenus (including menu items) object using standard functions provided in NlsMenu Pro to set menu and menu items property. Later sections will explain more about loading menu from HTML. The HTML Format Below is the format of HTML used to define menu:
<div id="menudiv">
<ul id="">
<li id=""><a href="url" target="" title=""><img src="itemicon"><span>item text</span></a>
<ul id="">
<li id=""><a href="url" target="" title="">item text</a></li>
<li id=""><a href="url" target="" title=""><span><strong><item></strong> text</span></a></li>
...
</ul>
</li>
<li id=""><a href="url" target="" title="">item text</a></li>
<li id=""><span>-</span></li>
<li id=""><a href="url" target="" title="">item text</a></li>
<li id=""><div>Custom item</div></li>
...
</ul>
<ul>
...
</ul>
....
</div>
The format is simple and easy to understand. Basically UL represents menubar/submenu and LI represents menu item.
Setting Menu and Item Property The HTML only defines menu structure and basic properties. To set other properties such as style prefix, menu orientation, etc, you can expose the menubar/submenu object from NlsMenuManager after creating menu structure by using loadMenuFromHTML() function. Below is the example:
<div id="menudiv" style='display:none'>
<ul id="main">
<li><a href="http://www.mydomain.com" title="Home">Home</a></li>
<li><a href="http://www.mydomain.com" title="Products">Products</a>
<ul id="subProducts">
<li><a href="http://www.mydomain.com" title="Computers">Computers</a>
<ul id="subComputers">
<li><a href="http://www.mydomain.com" title="Servers">Servers</a>
<li><a href="http://www.mydomain.com" title="Workstation and Desktop">Workstation and Desktop</a>
<li><a href="http://www.mydomain.com" title="NOtebook">Notebook</a>
</ul>
</li>
<li><a href="http://www.mydomain.com" title="Accessories">Accessories</a></li>
<li><a href="http://www.mydomain.com" title="Others" target="_blank">Others</a></li>
</ul>
</li>
<li><a href="http://www.mydomain.com" title="Services">Services</a>
<ul id="subServices">
<li><a href="http://www.mydomain.com" title="Networking Solution">Networking Solution</a></li>
<li><a href="http://www.mydomain.com" title="Custom Software Development">Custom Software Development</a></li>
</ul>
</li>
<li><a href="http://www.mydomain.com" title="About Us">About Us</a></li>
</ul>
</div>
<script>
var menuMgr = new NlsMenuManager("MyMenu");
menuMgr.defaultEffect="fade";
//Create menu from HTML
menuMgr.loadMenuFromHTML("menudiv", true, false);
//Get menubar object and set the properties
var menuBar = menuMgr.menubar;
menuBar.orient="H";
menuBar.stlprf="horz_";
...
//Get submenu object and set the properties
var products = menuMgr.getMenu("subProducts");
products.showIcon=true;
//Get item from products menu and set the properties.
var prodIt1 = products.getItemById("1");
prodIt1.ico = ["normalicon.gif", "hovericon.gif"];
//Render the menu
menuMgr.renderMenubar("menudiv");
</script>
As shown in example above, use NlsMenuManager.menubar to reference to menubar object and use NlsMenuManager.getMenu(mId) function to get reference to a submenu. For complete list of properties and functions supported by NlsMenu Pro, please check Reference. Also important from the example is the getItemById() function is used to get reference to a menu item. The item id '1' in the example is not defined in any items of Products menu, so where is it come from ? As described earlier, the item id is optional and if omitted, the menu will automatically assign sequence number to its items. The first item will have an id 1 and so on. Separator and Custom Item are also considered as menu item and therefore have an id too. If the id is defined, you can use the id to get reference to the item for example:
<li id="itProduct">Product</li>
To get reference to the item:
var prodIt1 = products.getItemById("itProduct");
Alternative Functions to Set Menu and Item Property NlsMenu Pro supports alternative functions to set menu and item property using JSON text.
Some special properties such as shadow and advanced borders can be specified in JSON text too. For example:
var menuBar=menuMgr.menubar;
menuBar.setProperties (
{
orient:"H", stlprf:"horz_", showIcon:true, showSubIcon:false,
shadow:new NlsMenuShadow("bottomright", "5px", "main"), //"main" is menubar id
customBorder:new NlsMenuBorder(true,true,true,true),
items: {
"2": {ico:["item2normal.gif","item2hover.gif"]}, //properties for Products item.
"3": {ico:["item3normal.gif","item3hover.gif"]}, //properties for Services item.
}
}
);
|