Javascript 未使用phonegap激发设备未就绪事件
我已经在main.js中编写了这段代码,并添加了这个文件index.html,但在android emulator事件上一切正常,但在设备就绪事件上,在cat日志上显示错误,未触发设备就绪事件。我不明白我的错误在哪里。这段代码有问题吗 main.jsJavascript 未使用phonegap激发设备未就绪事件,javascript,cordova,Javascript,Cordova,我已经在main.js中编写了这段代码,并添加了这个文件index.html,但在android emulator事件上一切正常,但在设备就绪事件上,在cat日志上显示错误,未触发设备就绪事件。我不明白我的错误在哪里。这段代码有问题吗 main.js $(document).ready(function () { // setup global error handler window.onerror = function (message, url, lineN
$(document).ready(function () {
// setup global error handler
window.onerror = function (message, url, lineNumber) {
console.log([lineNumber, url, message].join(" "));
return false;
};
window.isphone = false;
if(document.URL.indexOf("http://") === -1
&& document.URL.indexOf("https://") === -1) {
window.isphone = true;
}
if(window.isphone) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady();
}
console.log("document.ready");
console.log(window.location.hash);
// start the app
});
function onDeviceReady() {
app.init();
}
var app = app || { _timeStart: null };
app.init = function () {
//events binding
});
};
你需要重新排列你的代码
deviceready
应该是您附加和侦听的第一个事件,因为它是将触发的第一个事件,甚至在$(document.ready()
之前就触发了。Andrew我不明白您的意思,您可以更新代码中指定代码更改的位置吗。它应该是这样的:现在可以吗var mainApp={initialize:function(){this.bindEvents();},bindEvents:function(){document.addEventListener('devicerady',this.ondevicerady,true);},ondevicerady:function(){console.log('event Fired');app.init();},};与mainApp.initialize()类似时,我将从我的index.html before body标记调用此mainApp.initialize;