Appendix A
Answers to Exercises
This appendix provides the answers to Exercises found in chapters throughout this book.
CHAPTER 4 EXERCISE ANSWERS
Following are the answers to the exercises in Chapter 4.
Answer to Question 1
The following code snippet shows how you can open the Google homepage as soon as you detect that the PhoneGap application is online:
<!DOCTYPE html> <html> <head> <title>PhoneGap Google Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { // Register the event listeners document.addEventListener("online", isOnline, false); } function isOnline() { window.location = "http://www.google.com"; } </script> </head> <body> </body> </html>
Answer to Question 2
The following code snippet shows how you can display the current time when the device is ready:
<!DOCTYPE html> <html> <head> <title>PhoneGap Google Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { showDateTime(); } function showDateTime() { var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); var target = document.getElementById("time"); target.innerHTML("<b>" + hours + ":" + minutes + "</b>"); } </script> ...