WinJS: Hardware Back Button Pressed Event in Windows Phone 8.1 App

In this tutorial, we learn how to handle hardware back button pressed event in Windows Phone project that is built using HTML/JS/CSS (WinJS).

Step 1

Firstly, create a Windows Phone WinJS project. File > New Project > Other Languages > JavaScript > Windows 8 > Windows Phone > Blank App (Windows Phone). Name it “BackPress” and click OK.


Step 2

In the default.js file, add the following highlighted code snippet. Here ‘return true’ prevents the app from closing the app.


Step 3

Run the application in your device or emulator and tap the hardware button and you get the alert.


You can also get the complete project from GitHub