Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap deviceready vs document ready [duplicate]

I am experiencing issues with the phonegap device ready event. I am testing under iOS 6.0.

When device ready is fired, the DOM isn't ready. If I bind events to some DOM Elements in a devicereadyevent listener, I will receive no notifications because these elements do not exist at this early time.

So what are the best practices for waiting until BOTH have finished loading - DOM and phonegap?

like image 815
gorootde Avatar asked Oct 12 '12 18:10

gorootde


2 Answers

if your are using jquery try this

$(document).ready(function(){

    document.addEventListener("deviceready",onDeviceReady,false);       
});

function onDeviceReady(){
    //write your function body here

}

if your are using javascript only try this

if(document.readyState === "complete") {
  document.addEventListener("deviceready",onDeviceReady,false); 
}

function onDeviceReady(){
        //write your function body here

    }
like image 168
chandu Avatar answered Oct 10 '22 10:10

chandu


try something like this:

function onLoad(){
  document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady(){
  // Now safe to use the PhoneGap API
}

Then:

<body onload="onLoad()">

This will ensure the DOM is ready before the deviceready is called

like image 1
Dawson Loudon Avatar answered Oct 10 '22 10:10

Dawson Loudon