ajaybc ajaybc - 1 month ago 24x
Javascript Question

JQuery document.ready vs Phonegap deviceready

I am making a phonegap application with jquery. I am confused whether I should wrap my entire code inside JQuery's



or inside the deviceready event of phonegap like

document.addEventListener("deviceready", function(){

I am currently using
but I think I may encounter problems if I try to access some Phonegap API methods inside

Which is the best event to wrap my code in, document.ready or deviceready?


You should use the deviceready event to avoid funny things happening.

The docs state:

This is a very important event that every PhoneGap application should use.

PhoneGap consists of two code bases: native and JavaScript. While the native code is loading, a custom loading image is displayed. However, JavaScript is only loaded once the DOM loads. This means your web application could, potentially, call a PhoneGap JavaScript function before it is loaded.

The PhoneGap deviceready event fires once PhoneGap has fully loaded. After the device has fired, you can safely make calls to PhoneGap function.

Typically, you will want to attach an event listener with document.addEventListener once the HTML document's DOM has loaded.

Read the documentation here:http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html