JavaScript: Catch Windows Phone Hardware Backbutton

Tim Bernhard

JavaScriptAppHTML5ProgrammierungWindows Phone

This post is old. Codes, opinions and facts could be outdated.

Help to fix potential errors: GitHub

When programming a Windows Phone application with JavaScript, the "back" button at the bottom left of the smartphone can also be intercepted in order to carry out an alternative activity, for example, ending the application. In the following I would like to explain the procedure.

In the crucial HTML page, a WinJS Application Control element must be created with the parameter onbackclick: myFunction, where myFunction corresponds to the function that is called when the back button is clicked. This should look something like this:

<div data-win-control="WinJS.Application." data-win-options="{onbackclick : meineFunktion}" />

The EventHandler "backclick" can then be used. For example like this:

document.addEventListener("backclick", meineFunktion);

However, to suppress the default behavior, the termination of the application, true must be returned. A recommended use is in the file default.js:

app.onbackclick = function (evt) {
   meineFunktion();
   return true;
}
Webmentions

No webmentions found for this page.