16.11.2015 Views

PG2

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

38 Chapter 2 PhoneGap Development, Testing, and Debugging<br />

be working on Android, but not BlackBerry, so I used the alert to help confirm my<br />

suspicion and to help test different approaches as I attempted to fix the problem.<br />

The problem with this approach is that when you fill your buggy code with alerts,<br />

you’re constantly interrupting the application flow to dismiss the alerts as they<br />

come up. For a simple problem, this approach works pretty well, but when debugging<br />

more troublesome errors, you will need an approach that allows you to let the<br />

application run and then analyze what is happening in real time or after the application<br />

or a process within the application has completed, without interrupting the<br />

application. PhoneGap applications can do this through the JavaScript console<br />

object implemented by the WebKit browser rendering engine.<br />

Using the console object, developers can write messages to the browser’s console,<br />

which can be viewed outside of the running program through capabilities<br />

provided by the native SDKs or device simulators or emulators. The console<br />

object has scope at the window level, so it’s essentially a global object accessible<br />

by any JavaScript code within the application. WebKit supports several options;<br />

the most common ones used are as follows:<br />

• console.log("message");<br />

• console.warn("message");<br />

• console.error("message");<br />

Example 2-1 shows a sample application that illustrates the use of this feature.<br />

Example 2-1<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

function onBodyLoad() {<br />

document.addEventListener("deviceready", onDeviceReady,<br />

false);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!