Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Catching menu events in Symbian Web Runtime

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Platform(s): S60 5th Edition
Keywords: menu, menu.onShow, MenuItem.onSelect
Created: dekuykin (26 Feb 2009)
Last edited: hamishwillee (04 Oct 2012)



This code snippet demonstrates how to handle the onShow menu event. It shows the count of how many times a menu has been opened on-screen.

The snippet uses the WRT's menu object and the menu.onShow property to set the handler for menu events.

The property MenuItem.onSelect is used to set the handler for menu item selecting events.

Source file: Relevant HTML components

<p id="menuOpenCountPhrase">
The menu is opened <strong>0</strong> times.
<input id="menuOpenCount" type="hidden" value="0" />

Source file: JavaScript file

var menuItemReset = null;
var CMD_MENU_RESET = 11;
// Set menu's onShow event handler = incrementCounter;
// Creates the main menu
function createMenu() {
// ...
// Create the "Reset" menu item
menuItemReset = new MenuItem("Reset", CMD_MENU_RESET);
menuItemReset.onSelect = onMenuItemSelected;;;
// Set menu's onShow event handler = incrementCounter;
// Gets called when a menu item is selected
function onMenuItemSelected(menuId) {
switch (menuId) {
// Reset the counter of menu events
try {
document.getElementById("menuOpenCount").value = "0";
} catch (e) {
alert("Error: " + e);
// Reset the menu openings count phrase
try {
document.getElementById("menuOpenCountPhrase").innerHTML =
"The menu is opened <strong>0</strong> times.";
} catch (e) {
alert("Error: " + e);
* Increments counter of menu opening events.

function incrementCounter() {
// Get reference to counter value
try {
var counterLabel = document.getElementById("menuOpenCount");
} catch (e) {
alert("Error: " + e);
if (counterLabel == undefined) {
alert("Error: Menu events counter object is undefined!");
// Get current count of menu opening events
var menuOpenCount = Number(counterLabel.value);
// Increment counter
var menuOpenCountNew = menuOpenCount + 1;
counterLabel.value = String(menuOpenCountNew);
// Form the menu openings count phrase
try {
document.getElementById("menuOpenCountPhrase").innerHTML =
"The menu is opened <strong>" + menuOpenCountNew +
"</strong> times.";
} catch (e) {
alert("Error: " + e);


Every time the menu is opened the indicator on the screen will be updated.

Selecting the "Reset" menu item resets the counter value to "0".

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful to developers. The version of the WRT stub application used as a template in this snippet is v1.1.

This page was last modified on 4 October 2012, at 05:50.
47 page views in the last 30 days.