Introduction
Their are many events that we use in Apache Cordova or PhoneGap. Some of them are:
- deviceready
- pause
- resume
- online
- offline
- backbutton
- menubutton
- searchbutton
- startcallbutton
- endcallbutton
- volumedownbutton
- volumeupbutton
1. deviceready
When the Apache Cordova is fully loaded then this event fires. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS, Windows Phone 7 and Bada 1.2 & 2.x. It is a very important event that every Cordova application uses. When Cordova has fully loaded then this event fires and then we apply the EventListener once it is loaded. The syntax of using this is:
document.addEventListener ("deviceready", youCallbackFunction, false);
Example: In this example we see how to use this event:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// Now safe to use the Cordova API
}
2. pause
When the Cordova application is put into the background then this event fires. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7. The syntax is:
document.addEventListener ("pause" , yourCallbackFunction, false);
Example:
document.addEventListener("pause", onPause, false);
function onPause() {
// Handle the pause event
}
3. resume
When the Cordova appliaction is retrieved from the background then this event fires. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7. The syntax is:
document.addEventListener ("resume" , yourCallbackFunction, false);
Example:
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event
}
4. online
When the Cordova application is online then this event fires. When the application's network connection changes to online, the online event is fired. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7. The syntax is:
document.addEventListener ("online" , yourCallbackFunction, false);
Example:
document.addEventListener("online", onOnline, false);
function onOnline() {
// Handle the online event
}
5. offline
When the Cordova application is not connected to the internet or it is offline then the offline event is fired. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7. The syntax is:
document.addEventListener ("offline" , yourCallbackFunction, false);
Example:
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
}
6. backbutton
When the user presses the back button then this backbutton event is fired. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher) and Windows Phone 7. The syntax is:
document.addEventListener ("backbutton" , yourCallbackFunction, false);
Example:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
7. menubutton
When the user presses the menu button then this event is fired. The supported platforms are Android and BlackBerry WebWorks (OS 5.0 and higher). The syntax is:
document.addEventListener ("menubutton" , yourCallbackFunction, false);
Example:
document.addEventListener("menubutton", onMenuKeyDown, false);
function onMenuKeyDown() {
// Handle the back button
}
8. searchbutton
When the user presses the search button on Android then this event id is generated.The supported platform is only Android. The syntax is:
document.addEventListener ("searchbutton" , yourCallbackFunction, false);
Example:
document.addEventListener("searchbutton", onSearchKeyDown, false);
function onSearchKeyDown() {
// Handle the search button
}
9.startcallbutton
When the user presses the start call button then this event is fired. The supported platform is only BlackBerry WebWorks (OS 5.0 and higher). The syntax is:
document.addEventListener ("startcallbutton" , yourCallbackFunction, false);
Example:
document.addEventListener("startcallbutton", onStartCallKeyDown, false);
function onStartCallKeyDown() {
// Handle the start call button
}
10. endcallbutton
When the user presses the end call button then this event is fired. The supported platform is only BlackBerry WebWorks (OS 5.0 and higher). The syntax is:
document.addEventListener ("endcallbutton" , yourCallbackFunction, false);
Example:
document.addEventListener("endcallbutton", onEndCallKeyDown, false);
function onEndCallKeyDown() {
// Handle the end call button
}
11.volumedownbutton
When the user presses the volume down button then this event is generated. The supported platform is only BlackBerry WebWorks (OS 5.0 and higher). The syntax is:
document.addEventListener ("volumedownbutton" , yourCallbackFunction, false);
Example:
document.addEventListener("volumedownbutton", onVolumeDownKeyDown, false);
function onVolumeDownKeyDown() {
// Handle the volume down button
}
12. volumeupbutton
When the user presses the volume up button then this event is generated. The supported platform is only BlackBerry WebWorks (OS 5.0 and higher). The syntax is:
document.addEventListener ("volumeupbutton" , yourCallbackFunction, false);
Example:
document.addEventListener("volumeupbutton", onVolumeUpKeyDown, false);
function onVolumeUpKeyDown() {
// Handle the volume up button
}
Summary: In this article I discussed the various events types of Apache Cordova/PhoneGap.