0

I'm making a web application, and I have to make a menu that is repeated on multiple pages. But every time I make a change in any menu I have to edit all pages. Javascript may create a menu something like this (this is an occurrence):

var divMenu = document.getElementById( "menu" );
//create element <ul> to add to div menu.
var ulMenu = document.createElement("ul");
ulMenu.value =
  "<ul id='nav'>"
  + "<li class='current'><a href='/sytematic_inventory/views/administrador/index.jsp'>Inicio</a></li>"
  + "<li><a href=''>Inventarios</a></li> "
  + "</ul>";

divMenu.appendChild( ulMenu );

How is this possible?

3
  • Put it in a .js file and then include it in every page? Commented Dec 2, 2012 at 1:20
  • 3
    Please stop writing HTML in JS. Commented Dec 2, 2012 at 1:22
  • While you could use JavaScript to generate a menu on each page, it is a much better solution to use a server-side language to do this. Some of the fallbacks of a JavaScript-based approach: browsers with JavaScript disabled will not be able to see the menu; most search engines will not execute JavaScript and may not be able to spider your whole site. Commented Dec 2, 2012 at 1:27

3 Answers 3

3

Create a remote .js file and do something along the lines of the following:

(function(){

    // All items we'd like to add
    var navItems = [
        {href: 'http://google.com', text: 'Google'},
        {href: 'http://bing.com', text: 'Bing'},
        {href: 'http://stackoverflow.com', text: 'StackOverflow'}
    ];

    // A few variables for use later
    var navElem = document.createElement("nav"),
        navList = document.createElement("ul"), 
        navItem, navLink;

    navElem.appendChild(navList);

    // Cycle over each nav item
    for (var i = 0; i < navItems.length; i++) {
        // Create a fresh list item, and anchor
        navItem = document.createElement("li");
        navLink = document.createElement("a");

        // Set properties on anchor
        navLink.href = navItems[i].href;
        navLink.innerHTML = navItems[i].text;

        // Add anchor to list item, and list item to list
        navItem.appendChild(navLink);
        navList.appendChild(navItem);
    }

    // Set first list item as current
    navList.children[0].className = "current";

    // Add list to body (or anywhere else)
    window.onload = function () {
        document.body.appendChild(navElem);
    }

}());

Demo: http://jsfiddle.net/sDbff/3/

Then, in any page you'd like to include this, drop in a reference to it:

<!DOCTYPE html>
<html>
    <head>
        <title>My Site</title>
        <script src="navigation.js"></script>
    </head>
    <body>
        <p>Navigation will be appended here.</p>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Nice clean (no HTML) solution. Not crazy about the .innerHTML for setting the text, but that's just me. :-) Only thing I see missing is the class on the li element. ...but then I'm not sure which li should get it.
@user1689607 I could append a textNode to the anchor, but that would prevent the author from using any html within the anchor text. I missed the class on the list item; I'll toss that in via a update momentarily.
How is posible add a submenu in one menu ( navList )?.
@ChristianChaparroA You would have to modify your original navItems object to add a submenu property that itself has a series of items. You'd need a recursive function that creates a new navList for any submenu found within the current list item.
1

You need to create all the DOM items.

e.g.

var divMenu = document.getElementById( "menu" );
//create element <ul> to add to div menu.
var ulMenu = document.createElement('ul');
ulMenu.setAttribute('id', 'nav');
var liItem = document.createElement('li');
liItem.setAttribute('class', 'current');

ulMenu.appendChild(liItem);

var href = document.createElement('a');
href.setAttribute('href', '/sytematic_inventory/views/administrador/index.jsp');

liItem.appendChild(href);

href.appendChild(document.createTextNode('Inicio'));
// Ditto for the other liItem
document.getElementById('menu').appendChild(ulMenu);

Then put this into an external file. i.e.

<script type="text/javascript" src="menu.js"></script>

Comments

0

Put your javascript in an external file and then in your html pages you can link to the file:

<head>    
<script src="mymenu.js" type="text/javascript"></script>
</head>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.