Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1122793 of contextmenu

  • Revision slug: Web/HTML/Global_attributes/contextmenu
  • Revision title: contextmenu
  • Revision id: 1122793
  • Created:
  • Creator: Sebastianz
  • Is current revision? Yes
  • Comment Improved live sample formatting and output

Revision Content

{{HTMLSidebar("Global_attributes")}}

The contextmenu global attribute is the id of a {{HTMLElement("menu")}} to use as the contextual menu for this element.

A context menu is a menu that appears upon user interaction, such as a right-click. HTML5 now allows us to customize this menu. Here are some implementation examples, including nested menus.

Example

HTML

<body contextmenu="share">
  <menu type="context" id="share">
    <menu label="share">
      <menuitem label="Twitter" onclick="shareViaTwitter()"></menuitem>
      <menuitem label="Facebook" onclick="shareViaFacebook()"></menuitem>
    </menu>
  </menu>
  <ol>
    <li>
      Anywhere in the example you can share the page on Twitter and
      Facebook using the Share menu from your context menu.
    </li>
    <li contextmenu="changeFont" id="fontSizing">
      On this specific list element, you can change the size of the text
      by using the "Increase/Decrease font" actions from your context menu
    </li>
    <menu type="context" id="changeFont">
      <menuitem label="Increase Font" onclick="incFont()"></menuitem>
      <menuitem label="Decrease Font" onclick="decFont()"></menuitem>
    </menu>
    <li contextmenu="ChangeImage" id="changeImage">
      On the image below, you can fire the "Change Image" action
      in your Context Menu.<br />
      <img src="https://developer.mozilla.org/media/img/promote/promobutton_mdn5.png"
          contextmenu="ChangeImage" id="promoButton" />
      <menu type="context" id="ChangeImage">
        <menuitem label="Change Image" onclick="changeImage()"></menuitem>
      </menu>
    </li>
  </ol>
</body>

JavaScript

function shareViaTwitter() {
  window.open("https://twitter.com/intent/tweet?text=" +
      "Hurray! I am learning ContextMenu from MDN via Mozilla");
}

function shareViaFacebook() {
  window.open("https://facebook.com/sharer/sharer.php?u=" +
      "https://developer.mozilla.org/en/HTML/Element/Using_HTML_context_menus");
}

function incFont() {
  document.getElementById("fontSizing").style.fontSize = "larger";
}

function decFont() {
  document.getElementById("fontSizing").style.fontSize = "smaller";
}

function changeImage() {
  var index = Math.ceil(Math.random() * 39 + 1);
  document.images[0].src =
      "https://developer.mozilla.org/media/img/promote/promobutton_mdn" +
      index + ".png";
}

Result

{{EmbedLiveSample("Example", "100%", 400)}}

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', "forms.html#attr-contextmenu", "contextmenu")}} {{Spec2('HTML WHATWG')}} No change from latest snapshot, {{SpecName('HTML5.1')}}
{{SpecName('HTML5.1', "interactive-elements.html#context-menus", "contextmenu")}} {{Spec2('HTML5.1')}} Snapshot of {{SpecName('HTML WHATWG')}}, initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}}[1] {{CompatGeckoDesktop(9)}} {{CompatNo}} {{CompatNo}}[1] {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatGeckoDesktop(20)}} {{CompatNo}} {{CompatNo}} {{CompatUnknown}}

[1] An experimental implementation is available via the command line option --enable-blink-features=ContextMenu. Until Chrome 52 and Opera 39 it was additionally available by enabling the Experimental Web Platform features option, but got removed from that due to a Web compatibility issue. Support for this feature is requested in {{ChromeBug(87553)}}.

See also

Revision Source

<div>{{HTMLSidebar("Global_attributes")}}</div>

<p>The <code><strong>contextmenu</strong></code> <a href="/en-US/docs/Web/HTML/Global_attributes">global attribute</a> is the <a href="/en-US/docs/Web/HTML/Global_attributes/id"><strong>id</strong></a> of a {{HTMLElement("menu")}} to use as the contextual menu for this element.</p>

<p>A <em>context menu</em> is a menu that appears upon user interaction, such as a right-click. HTML5 now allows us to customize this menu. Here are some implementation examples, including nested menus.</p>

<h2 id="Example">Example</h2>

<h3 id="HTML">HTML</h3>

<pre class="brush:html; highlight:[1,10,15]">
&lt;body contextmenu="share"&gt;
  &lt;menu type="context" id="share"&gt;
    &lt;menu label="share"&gt;
      &lt;menuitem label="Twitter" onclick="shareViaTwitter()"&gt;&lt;/menuitem&gt;
      &lt;menuitem label="Facebook" onclick="shareViaFacebook()"&gt;&lt;/menuitem&gt;
    &lt;/menu&gt;
  &lt;/menu&gt;
  &lt;ol&gt;
    &lt;li&gt;
      Anywhere in the example you can share the page on Twitter and
      Facebook using the Share menu from your context menu.
    &lt;/li&gt;
    &lt;li contextmenu="changeFont" id="fontSizing"&gt;
      On this specific list element, you can change the size of the text
      by using the "Increase/Decrease font" actions from your context menu
    &lt;/li&gt;
    &lt;menu type="context" id="changeFont"&gt;
      &lt;menuitem label="Increase Font" onclick="incFont()"&gt;&lt;/menuitem&gt;
      &lt;menuitem label="Decrease Font" onclick="decFont()"&gt;&lt;/menuitem&gt;
    &lt;/menu&gt;
    &lt;li contextmenu="ChangeImage" id="changeImage"&gt;
      On the image below, you can fire the "Change Image" action
      in your Context Menu.&lt;br /&gt;
      &lt;img src="https://developer.mozilla.org/media/img/promote/promobutton_mdn5.png"
          contextmenu="ChangeImage" id="promoButton" /&gt;
      &lt;menu type="context" id="ChangeImage"&gt;
        &lt;menuitem label="Change Image" onclick="changeImage()"&gt;&lt;/menuitem&gt;
      &lt;/menu&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/body&gt;
</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush:js">
function shareViaTwitter() {
  window.open("https://twitter.com/intent/tweet?text=" +
      "Hurray! I am learning ContextMenu from MDN via Mozilla");
}

function shareViaFacebook() {
  window.open("https://facebook.com/sharer/sharer.php?u=" +
      "https://developer.mozilla.org/en/HTML/Element/Using_HTML_context_menus");
}

function incFont() {
  document.getElementById("fontSizing").style.fontSize = "larger";
}

function decFont() {
  document.getElementById("fontSizing").style.fontSize = "smaller";
}

function changeImage() {
  var index = Math.ceil(Math.random() * 39 + 1);
  document.images[0].src =
      "https://developer.mozilla.org/media/img/promote/promobutton_mdn" +
      index + ".png";
}</pre>

<h3 id="Result">Result</h3>

<p>{{EmbedLiveSample("Example", "100%", 400)}}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "forms.html#attr-contextmenu", "contextmenu")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>No change from latest snapshot, {{SpecName('HTML5.1')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', "interactive-elements.html#context-menus", "contextmenu")}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td>Snapshot of {{SpecName('HTML WHATWG')}}, initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}<sup>[1]</sup></td>
   <td>{{CompatGeckoDesktop(9)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop(20)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] An experimental implementation is available via the command line option <code>--enable-blink-features=ContextMenu</code>. Until Chrome 52 and Opera 39 it was additionally available by enabling the <em>Experimental Web Platform features</em> option, but got removed from that due to a <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=412945">Web compatibility issue</a>. Support for this feature is requested in {{ChromeBug(87553)}}.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>All <a href="/en-US/docs/Web/HTML/Global_attributes">global attributes</a></li>
 <li>{{domxref("HTMLElement.contextMenu")}}</li>
</ul>
Revert to this revision