Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  197] [ 5]  / answers: 1 / hits: 61932  / 12 Years ago, tue, september 25, 2012, 12:00:00

I am making a phonegap application with jquery. I am confused whether I should wrap my entire code inside JQuery's $(document).ready() like



$(document).ready(function(){
//mycode
});


or inside the deviceready event of phonegap like



document.addEventListener(deviceready, function(){
//mycode
});


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



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


More From » jquery

 Answers
42

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


[#82924] Sunday, September 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;