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);

Join the conversation! Your thoughts help the community grow.