Click here to Skip to main content
15,074,394 members
Articles / Web Development / XHTML
Posted 16 Aug 2008


15 bookmarked

Menu and Sub Menu Using JavaScript

Rate me:
Please Sign up or sign in to vote.
3.20/5 (4 votes)
16 Aug 2008LGPL32 min read
Simple JavaScript for creating a menu and sub menus using the IE window.createpopup function.

Image 1

Image 2


The following code example helps to create a popup style menu and a submenu in HTML using simple JavaScript. However, the approach is slightly different from what I could gather from various open source codes available on the Internet.

Some of the keywords which relate to this article in my opinion could be: menus, JavaScript popup menu, menu and sub-menu, window.createpopup, div style menus, window without toolbar, window without menubar, parent, child, grandparent window, and DHTML popup.


I was looking to port a piece of code which displayed a few menu buttons on a user's window to a popup style menu and submenu on the click of a single button. This led me to experiment with options such as div tags, tooltip style popup,, and finally window.createpopup.

Instead of a div tag, the use of window.createpopup enables the menu and submenu to be displayed even if the parent window is shrunk/resized to a small screen area, i.e., the menu and submenu shall be displayed over and outside the boundaries of the current parent browser window, and not restricted to just the current browser window size/area.

Using the code

Note: Currently, such a JavaScript object/method does not exist for any version of Mozilla Firefox browsers. Only IE/Opera browsers have this functionality.

Without further delay, here is the piece of code which has suitable comments explaining a simple approach. However, more decoration needs to be done for the menu and sub menus to get the look and feel of menus. By this, I mean the innerHTML could take on the code of a table HTML tag.


// Code example.

<script language="javascript">

// Create a new javascript object to manage menu & submenu. 
// Pass reference to window object.
window.myobj = new Object();

// function which will be called from main browser window.
function showfunction() 
    var offsetWidth = 100; 
    var offsetHeight = 60;

    // Menu popup reference stored in window object's variable p.
    window.p = window.createPopup();

    //string substitution for Menu contents.This could be a document or 
    // body tags from HTML. Done to avoid parsing double quote problems.
    p.document.body.innerHTML = strMenu; = 'solid windowframe 1px'; + 99,screenTop + 25,200,65);

// variable to hold innerHTML of Menu Popup. 
var strMenu ="<b onclick=\"( ";
strMenu += window.myobj.myFunctionObj;  // using object as function.
strMenu += ")()\">Hello World! I am Menu popup.</b>";

// function defined which is part of menu popup innerHTML.
myobj.myFunctionObj= function(){

    // Now create sub menu popup as part of menu's innerHTML.
    // Sub Menu popup reference stored in window object's variable.
    window.l = window.createPopup();

    // We use string substitution to enable parsing of double 
    // quotes present in innerHTML. 
    l.document.body.innerHTML = parent.window.strSubMenu; = 'solid windowframe 1px';,screenTop + 45,200,50);


// similar string substitution and function object 
// usage for sub menu.
var strSubMenu ="<b onclick=\"( ";
strSubMenu +=  window.myobj.myFunctionObjSub;
strSubMenu += ")()\">I am the sub Menu popup.</b>";

var strCloseWindow = "parent.window.parent.window.close();"; 
var strAlterDiv = "parent.parent.document.
getElementById('div1').innerHTML = 'New div tag text.'";   

// function assigned to object for parsing during creation of sub menu.
myobj.myFunctionObjSub = function(){
    // How do we access main window vaiables?
    //action alter of Div tag.
    //action closing of grandpa window i.e. browser window.


Some text Some text Some text Some text Some tex....Some more text.
<input type="button" name="btn1" value="Click Me Image Btn" 
<div id="div1" style="width=200px; height=100px; padding:4px ;"> 
    This div element shall be altered on click of submenu item.

Points of interest

Now, a piece to ponder over: since the references to the menu and submenus are available within the window object, can we try to popup/display the submenu without the need to call Show on the menu object reference? Try it!


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Software Developer (Senior)
United States United States
I have worked on various Microsoft Technologies in Web Applications for major Telecom Service Provider & Insurance company in Europe. Languages include C#, ASP, ASP .Net, SQL Server, SSIS & XML.

Comments and Discussions

GeneralMy vote of 2 Pin
arunk52527-May-10 15:52
Memberarunk52527-May-10 15:52 
Generalunable to run a code Pin
schandrashekhar18-Jan-09 20:05
Memberschandrashekhar18-Jan-09 20:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.