« Code Highlighting on MovableType | Main | The Ruby-fitbit API has been updated »

Javascript document.addEventListener PhoneGap deviceready issues

I had some issues with the deviceready callback. I had seen some similar posts, and figured I could save someone some time. Basically when using document.addEventListener order matters a lot. It all comes down to exactly when this line is loaded and what is already defined.

document.addEventListener("deviceready", deviceSetup, true);

We have custom JS, and originally it was loaded second, so the
deviceready event was fired before, the listener was set.

        <script type="text/javascript" charset="utf-8" src="lscustom.js"></script> 
        <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 

After that I was still having an issue and it turns out the method you
define register as the callback needs to be defined before you add the
listener.
So having this in our lscustom.js works.

var deviceSetup = function(){ 
  console.log("Device Setup"); 
}; 

document.addEventListener("deviceready", deviceSetup, true);
While, the below fails.
document.addEventListener("deviceready", deviceSetup, true);
var deviceSetup = function(){
console.log("Device Setup");
};

Anyways since I ended up wasting a bit of time searching and debugging this issue, I figured I would share my debugging session.

Post a comment


Type the characters you see in the picture above.

Web 2.0 craziness

View Dan Mayer's profile on LinkedIn


I Power Seekler
I Power Seekler

www.flickr.com
This is a Flickr badge showing public photos and videos from mayer_dan. Make your own badge here.

Creative Commons License
This weblog is licensed under a Creative Commons License.