Frames Example | Page 3 |
Here is the code for displayMenu(). Note that a global variable called lastMenu is defined. It is used to keep track of which menu was opened last so it can be closed before another one is opened. It's not necessary to do this but it prevents having several menus open on the page at the same time.
//--------------------------------------------------------------------------- // Code for opening a menu in another frame. //--------------------------------------------------------------------------- var lastMenu = null; // Points to the last menu opened. function displayMenu(frameName, menuName) { var menu; var x, y; // Close the last menu opened, if it's still accessable. if (lastMenu != null) { if (isMinNS4 && lastMenu.isOpen) lastMenu.close(); if (isMinIE4 && !isMinIE5) lastMenu.close(); if (isMinIE5) eval('try { lastMenu.close(); } catch(ex) {};'); } // Exit if given frame doesn't exist. if (!parent.frames[frameName]) return; // Get a handle to the menu. If it's not found, exit. menu = eval("parent.frames['" + frameName + "']." + menuName); if (!menu) return; // Set the menu position based on the mouse position in this frame and any // scrolling offsets. x = parent.frames[frameName].getPageScrollX(); y = mouseY + parent.frames[frameName].getPageScrollY(); if (!menu.isOpen) menu.open(x, y); // Save the menu so it can be closed before another is opened. lastMenu = menu; } First, it checks the lastMenu variable. If it's pointing to a valid menu it is closed. Likewise, it checks to see if the named frame exists. Otherwise, an error would occur when attempting to access it. Then it attempts to access the menu by referring to the given variable name in that frame. Again, if it doesn't exist the function exits. If all the above succeeds, it will now have a handle to the menu in the right-hand frame. The next trick is to position it on the page so that will appear right next to the link in the left-hand frame that called the function...
Prev Next |