I’m trying to dynamically load the phonegap javascript file (so that I can choose not to load it in debug mode when I’m using Ripple) but I’m running in to some issues.
I load the jquery and jquerymobile javascript libraries using a normal script tag. In another script block, I do:
function onDeviceReady() {
alert("Device Ready!");
}
$(document).ready(function() {
alert("doc ready!");
$.getScript("js/phonegap.0.9.5.1.js", function() {alert("Got Phonegap!");});
document.addEventListener("deviceready", onDeviceReady, false);
});
This code alerts that it “Got Phonegap!” but never alerts “Device Ready”. Using jsconsole.com, I can see that the PhoneGap javascript object exists. However, trying to call device.uuid (or other simple phonegap API calls) fails. It’s almost like PhoneGap didn’t fully initialize. Doesn’t seem like that should be the case though. Am I missing something? Thanks!
I was facing the similar issue where in which I need to load the PhoneGap and dependent plugin files based on the platform type. I went through the PhoneGap source and found that it uses the windows/browser events to load and prepare the objects. If I call the browser events manually then it initializes the PhoneGap objects (API and Plugins) I needed to run my application.
The following code which is using Yabble has worked for me now:
Both Device info and Plugin calls is working fine on Android. Although I have not checked all the PhoneGap API but as of now I need only these two to work and they are working.
Edit
In Phonegap 1.5/Cordova,
PhoneGap.onPhoneGapInit.fire();is not available due to API change. In my current test most of the required objects are available without any change after loading the JS dynamically. Updated test is available at this gist – Cordova Lazy Load Test