Add Top Menu Entries From JavaScript
Last modified by Manuel Leduc on 2026/06/02 17:54
| Adds new entries to the top menus, from a JavaScript extension |
| Type | Snippet |
| Category | |
| Developed by | |
| Rating | |
| License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Code
Paste and adapt the following code in a JavaScript eXtension object to dynamically inject a new menu in the XWiki top menu bar.
(function(){
document.observe("dom:loaded", function(){
var topMenu = $('actionmenu').down('.leftmenu');
if(typeof topMenu != "undefined") {
var aMenu = topMenu.appendChild(
new Element("div", {
'class':'topmenuentry',
'onmouseout' : "hidesubmenu(this);",
'onmouseover' :"showsubmenu(this);"
}).update(
new Element("a", {
'class':'tme',
'style' : 'cursor:default;'
}).update("Extra !")
)
);
var colon = new Element("span", {'class' : 'hidden menucolon'}).update(":");
$(aMenu).insert( {'bottom': colon });
var menuEntries =
new Element("span", {'class' : 'submenu hidden'}).update(
new Element("span", {'class':'submenuitem'}).update(
new Element("a").update("Say Hello").observe("click", sayHello)
)
);
$(aMenu).insert( {'bottom': menuEntries });
menuEntries.insert( {'bottom': new Element("span", {'class' : 'submenuseparator'}) });
var menuEntry2 =
new Element("span", {'class':'submenuitem'}).update(
new Element("a").update("Do Something else").observe("click", doSomethingElse)
);
menuEntries.insert( {'bottom': menuEntry2 });
}
});
function sayHello() {
alert("Hello");
}
function doSomethingElse() {
alert("Something Else !");
}
})();Result
