HTML DOM Menu Object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Menu Object in HTML represents the <menu> element.

NOTE<menu> element only supported in Mozilla Firefox Browser.

Syntax

Following is the syntax −

Creating a <menu> element

var menuObject = document.createElement(“MENU”)

Example

Let us see an example for HTML DOM Menu element −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Menu Object</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form contextmenu="MENU">
<fieldset>
<legend>Menu-Object</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com/aboutUs">
<div id="divDisplay"></div>
</fieldset>
<menu type="context"
id="MENU">
<menuitem label="Get URL"
onclick="contextFunction(1);">
</menuitem>
<menuitem label="Get Hostname"
onclick="contextFunction(2);">
</menuitem>
</menu>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function gethref(){
      divDisplay.textContent = 'URL Path: '+location.href;
   }
   function getHostname(){
      divDisplay.textContent = 'Hostname: '+location.hostname;
   }
   function contextFunction(role){
      if(role === 1)
         this.gethref();
      else
         this.getHostname();
   }
</script>
</body>
</html>

Output

This will produce the following output −

Right clicking on form −

After clicking ‘Get Url’ menu item in context menu −

After clicking ‘Get Hostname’ menu item in context menu −

raja
Published on 31-Jul-2019 17:43:17
Advertisements