Revision as of 13:45, 26 February 2009 by croozeus (Talk | contribs)

Catching menu events in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search

Article Metadata
Tested with
Devices(s): Nokia 5800 XM
Platform(s): S60 5th Edition
Keywords: menu, menu.onShow, MenuItem.onSelect
Created: (26 Jan 2008)
Last edited: croozeus (26 Feb 2009)


This code snippet demonstrates how to handle onShow menu event. It shows the count how many times menu was opened on screen.

It uses JavaScript WRT menu object and menu.onShow property to set the handler for menu events. Property MenuItem.onSelect is using to set handler for menu item selecting events.

Source file: Relevant HTML components

    <p id="menuOpenCountPhrase">
Menu was opened <b>0</b> times!
<input id="menuOpenCount" type="hidden" value="0"/>

Source file: JavaScript file

// Menu items
var menuItemCancel = null;
var menuItemReset = null;
// Menu id's
var CMD_MENU_RESET = 11;
window.onload = init;
// Initializes the widget
function init() {
// Creates the main menu
function createMenu() {
// Create the "Cancel" menu item
menuItemCancel = new MenuItem("Cancel", CMD_MENU_CANCEL);
menuItemCancel.onSelect = onMenuItemSelected;
// Create the "Reset" menu item
menuItemReset = new MenuItem("Reset", CMD_MENU_RESET);
menuItemReset.onSelect = onMenuItemSelected;
// Show right softkey
// Set menu's onShow event handler
window.menu.onShow = incrementCounter;
// Gets called when a menu item is selected
function onMenuItemSelected(menuId) {
switch (menuId) {
// Do nothing.
// 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 =
"Menu was opened <b>0</b> 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 =
"Menu was opened <b>" + menuOpenCountNew + "</b> times!";
} catch (e) {
alert("Error: " + e);


View of launched snippet contains information - how many times menu was opened. Every time the menu will be opened - indicator on screen will be updated. On click the "Reset" menu item counter is reseted to value "0".

Supplementary material

  • You can test the handling of menu event in action in a simple, executable application into which this code snippet has been patched. The executables and source files are available for download at: Media:Catching_menu_events_in_WRT.zip.
  • You can examine all the changes that are required to implement the above mentioned features in an application. The changes are provided in unified diff and color-coded diff formats: link_to_patchs
120 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.