You had one job pic.twitter.com/XYEvgtd1MR
— Burke H✪lland (@burkeholland) March 16, 2013
A door leading to a 20 foot drop that ends in pavement is obviously a relic from a remodel with no place in the natural order of things anymore. However, sometimes we have to sacrifice less important features in order to get support for more critical ones. In this case, they may have chosen to raise the ceiling instead of having a second story space. The door is the painful reminder.
The web is all about sacrificing "perfect" in order to get "working". If there was ever a place in web development where we see this played out, it's with the Context Menu.
Jobs and his band of rebels at NEXTSTEP took the idea a step further and allowed the menu to be brought up with a right mouse click which allowed the menu to be summoned right where the mouse was located.
Microsoft added their own bit of ingenuity when the allowed flyout and sub-menus in Word. Windows 7 changed the menu again, yet subtly, by activating the menu on release of the mouse button requiring two clicks to select a menu item. This was news to me as I have always clicked twice, but it is possible still on OS X to activate the menu with a right click and then select an item all in the same "click".
The trouble with Context Menus in browsers is that the browser has quickly morphed from a way to post Live Journal entries into a runtime for applications. That means that we expect the applications that are running inside of the browser to behave like desktop ones do, and desktop apps have context menus. Essentially, we want a application with context menus within an application with context menus.
<menu> element that have a type of context and then referencing the id of that element with the contextmenu attribute wherever you want that context menu to appear.
<menu id="customMenu">
<menuitem label="Share On Live Journal" onclick="blog()"></menuitem>
<menuitem label="Check AOL Email" onclick="blog()"></menuitem>
<menuitem label="Publish To GeoCities" onclick="blog()"></menuitem>
</menu>
<div contextmenu="customMenu">
<h1>Native Context Menu?</h1>
</div>
Didn't work for you? That would be because native context menus are only supported in FireFox. Here - I included a GIF of it for you so you won't feel completely left out.
Notice that the commands are simply appended to the top of the list. I find this a tad noisy, although the spec notes that maybe those default system commands would be collapsed by default, which I think is a much more desirable experience.
It doesn't look like any other browser vendors plan on implementing this anytime soon. Why is that? I'm not entirely sure, but Addy Osmani noted that the FireFox implementation actually differs from the spec. However, when I looked at the WHATWG spec, everything looks legit to me. Perhaps there is a different standard with the W3C.
The nice thing about standards is that you have so many to choose from — Andrew S. TanenbaumDavid Walsh points out that this custom context menu in FireFox will work even if JavaScript is disabled. This is a problem since those menu items are rather dependent on JavaScript in order to work. I don't see that as a reason not to implement them though. To add a twist to this story, Chrome actually does support custom context menus, just not on standard browser pages.
<ul id="menu">
<li>Share On Live Journal</li>
<li><img id="aol" src="http://icons.iconarchive.com/icons/ncrow/mega-pack-2/256/AOL-icon.png">Email with AOL</li>
<li>Publish To GeoCities</li>
</ul>
<div id="target">
<h1>Right-Click Me For Some Options</h1>
</div>
(function() {
$('#menu').kendoContextMenu({
target: '#target',
select: function(e) {
alert('pfffft');
}
});
}());
By default Kendo UI Context Menus have a vertical orientation, but this is JavaScript, so why not have a horizontal one?
Of course, Context Menus can have nested menus just like the native ones. That's accomplished simply by nesting lists. Adding separators is done by including a class of k-separator on an li.
Since it's all JavaScript, we can even control how the thing is summoned. We can have it shown on a regular click, right click, or any other event.
Burke Holland is a web developer living in Nashville, TN and was the Director of Developer Relations at Progress. He enjoys working with and meeting developers who are building mobile apps with jQuery / HTML5 and loves to hack on social API's. Burke worked for Progress as a Developer Advocate focusing on Kendo UI.