HTML onshow Attribute

The obsolete HTML onshow attribute was an event handler associated with the former HTML context-menu system. It was intended to run JavaScript when a context menu defined with a <menu> element was about to be displayed.

Historical Syntax

The onshow attribute contained JavaScript that was intended to run when a context menu was shown:

<menu type="context" id="example-menu" onshow="prepareMenu()">
  ...
</menu>

The attribute was part of an older context-menu feature and was not a general-purpose event for detecting whenever an ordinary HTML element became visible.

Historical Elements

The obsolete onshow event handler was associated with the former context-menu use of the <menu> element.

Element Historical Purpose
<menu> Ran script when an associated context menu was about to be displayed.

Historical Use

Older HTML context-menu proposals allowed a webpage to define a menu and associate it with another element. The onshow event handler could be used to run code immediately before that menu was displayed.

<div contextmenu="example-menu">
  Right-click this area.
</div>

<menu type="context" id="example-menu" onshow="prepareMenu()">
  ...
</menu>

A script could theoretically use the event to prepare or update menu commands before the user saw them. The feature depended on the old HTML context-menu system, including the now-obsolete contextmenu attribute.

Why It Became Obsolete

The HTML context-menu mechanism associated with onshow was removed from modern HTML. Once that mechanism was removed, the specialized event handler no longer had a role in current HTML.

Modern webpages can respond to user interactions through JavaScript events and can build accessible interface controls with current HTML elements. A custom context menu, when one is genuinely necessary, requires a different implementation rather than replacing onshow with another HTML attribute.

Modern Alternative

There is no direct modern replacement for the onshow attribute. Remove the obsolete context-menu markup and use current HTML controls whenever possible.

Historical HTML

<menu type="context" id="example-menu" onshow="prepareMenu()">
  ...
</menu>

Modern Approach

If an application needs to react to a user's context-menu action, JavaScript provides the contextmenu event:

const element = document.querySelector(".example");

element.addEventListener("contextmenu", function(event) {
  // Respond to the context-menu action when needed.
});

This JavaScript event is not a direct replacement for onshow. It detects a context-menu interaction with an element, while the old onshow handler belonged specifically to HTML's former custom context-menu system.

Legacy Browser Support

The onshow attribute depended on the former HTML context-menu feature and did not provide a dependable cross-browser method for building context menus.

Modern webpages should not rely on onshow, contextmenu attributes, or the obsolete context-menu form of the <menu> element. Use current HTML and JavaScript event handling for interactive interfaces.

For additional information about historical browser compatibility, visit Can I Use? . Search for the HTML element or attribute you want to check. Search results can include related HTML features, element attributes, APIs, and other technologies, so select the result that most closely matches the legacy feature you are researching.

Other obsolete attributes associated with older HTML event and context-menu features include:

  • contextmenu - Historically associated an element with a context menu.
  • event - An obsolete attribute historically used with older script event-binding mechanisms.

Specifications

The onshow event handler associated with the former HTML context-menu system is obsolete and should not be used in modern HTML. See the WHATWG HTML Living Standard for information about obsolete and non-conforming features.