O'Reilly logo

Beginning PhoneGap by Thomas Myer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required