Each element has anassigned context menu, which can be null. If an elementAhas acontextmenup553
attribute, and there is an element with
the ID given byA'scontextmenup553
attribute's value inA'stree
, and the first such element intree order
attribute is in thepopup menup549
state, thenA'sassigned context menup554
is that element. Otherwise, ifAhas a parent element, thenA'sassigned
is theassigned context menup554
of its parent element. Otherwise,A'sassigned context menup554
When an element's context menu is requested (e.g. by the user right-clicking the element, or pressing a context menu key), the user agent must
apply the appropriate rules from the following list:
If the user requested a context menu using a pointing device
The user agent mustfirep44
event with the namecontextmenup1154
, that bubbles and is cancelable, and that uses the
interface, at the element for which the menu was requested. The context information of the event must be initialised to the
same values as the lastMouseEvent
user interaction event that was fired as part of the gesture that was interpreted as a request for the
The user agent mustfire a synthetic mouse event namedcontextmenup854
that bubbles and is cancelable at the element for which the
menu was requested.
The default action of thecontextmenup1154
event depends on whether or not the element for which the menu was requested has a non-null
assigned context menup554
when the event dispatch has completed, as follows.
If theassigned context menup554
of the element for which the menu was requested is null, the default action must be for the user agent to show its
default context menu, if it has one.
Otherwise, letsubjectbe the element for which the menu was requested, and letmenube theassigned context menup554
event's dispatch has completed. The user agent mustfirep44
event with the nameshowp1155
atmenu, using the
interface, with therelatedTargetp556
attribute initialised tosubject. The event must be cancelable.
event) is not canceled, then the user agent mustbuild and showp549
the menu formenuwithsubjectas the subject.
The user agent may also provide access to its default context menu, if any, with the context menu shown. For example, it could merge the menu
items from the two menus together, or provide the page's context menu as a submenu of the default menu. In general, user agents are encouraged
to de-emphasise their own contextual menu items, so as to give the author's context menu the appearance of legitimacy — to allow documents to
feel like "applications" rather than "mere Web pages".
User agents may provide means for bypassing the context menu processing model, ensuring that the user can always access the UA's default
context menus. For example, the user agent could handle right-clicks that have the Shift key depressed in such a way that it does not fire the
event and instead always shows the default context menu.
ThecontextMenuIDL attribute mustreflectp89
This adds two items to the control's context menu, one called "Pick random name", and one called "Prefill other fields based on name".
They invoke scripts that are not shown in the example above.
Typically, therefore, the firing of thecontextmenup1154
event will be the default action of amouseup
event. The exact sequence
of events is UA-dependent, as it will vary based on platform conventions.
In this example, an image of cats is given a context menu with four possible commands:
<img src="cats.jpeg" alt="Cats" contextmenu=catsmenu>
<menu type="context" id="catsmenu">
<menuitem label="Pet the kittens" onclick="kittens.pet()">
<menuitem label="Cuddle with the kittens" onclick="kittens.cuddle()">