问题描述
我正在开发一个PhoneGap应用程序,我希望能够在Chrome中而不是在手机上调试它。但是,我做一个onDeviceReady()函数中的代码初始化,当PhoneGap触发deviceready事件时触发。由于Chrome不会触发此事件,因此我的代码不会被初始化。
I'm developing a PhoneGap application and I'd like to be able to debug it in Chrome rather than on the phone. However, I do the initialization of my code in an onDeviceReady() function that is triggered when PhoneGap fires the "deviceready" event. Since Chrome doesn't fire this event, my code isn't ever initialized.
这是代码的缩减版本:
var dashboard = {};
$(document).ready(function() {
document.addEventListener("deviceready", dashboard.onDeviceReady, false);
});
dashboard.onDeviceReady = function() {
alert("hello!"); //this is never fired in Chrome
};
我尝试使用代码,基本上只是执行以下操作:
I've tried using the StopGap code, which basically just does the following:
var e = document.createEvent('Events');
e.initEvent("deviceready");
document.dispatchEvent(e);
但是当我在Chrome javascript控制台中运行该代码时,hello触发。我做错了什么?
But when I run that code in the Chrome javascript console, the "hello" alert still doesn't trigger. What am I doing wrong? Or does chrome just not support firing "custom" events like deviceready?
推荐答案
将此代码添加到您的onLoad处理函数:
Add this code to your onLoad handler function:
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady();
}
事件deviceready在cordova.js中触发,因此我不知道一种在应用程序代码中检测此事件的存在的方法。
Event "deviceready" is fired in cordova.js so I don't know a way to detect existence of this event in application code.
这篇关于如何在Chrome浏览器中打开deviceready事件(尝试调试phonegap项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!