Javascript 如何等待Cordova和jQuery加载它们的库?

Javascript 如何等待Cordova和jQuery加载它们的库?,javascript,jquery,cordova,dom-events,Javascript,Jquery,Cordova,Dom Events,jQuery具有“就绪”功能: $(function() {...}); cordova文档说要添加一个事件侦听器: document.addEventListener("deviceready", onDeviceReady, false(); function onDeviceReady(){...} 我是否需要调用这两个函数以确保在使用引用它们的任何代码之前加载这两个库? 嵌套它们是否有意义,即: $(function(){ onDeviceReady(){...} } 只是

jQuery具有“就绪”功能:

$(function() {...});
cordova文档说要添加一个事件侦听器:

document.addEventListener("deviceready", onDeviceReady, false();
function onDeviceReady(){...}
我是否需要调用这两个函数以确保在使用引用它们的任何代码之前加载这两个库?

嵌套它们是否有意义,即:

$(function(){
    onDeviceReady(){...}
}

只是想找出最好的方法,也许我想得太多了,JS文件是按顺序加载的。因此,在HTML中,只需将.js文件放在jQuery和cordova之后,在加载这两个文件之前,它不会运行

<script src="jQuery"></script>
<script src="cordova"></script>
<script src="yourScript"></script> <!-- this won't run before both jQ and cordova are loaded -->

这将起作用,并且go()将在加载所有内容时启动,但是我真的不确定您是否真的需要所有这些内容,是否有简单的方法来完成这项工作。

将document.ready放入deviceready中:

var onDeviceReady = function (){
    jQuery(document).ready(function (){
        // go!
    });
};

document.addEventListener("deviceready", onDeviceReady, false);
jQuery是的代理,表示文档已被解析,可以安全地进行查询或操作


jQuery本身(
jQuery
$
)在库完成执行后立即可用;你不需要做任何特殊的事情来等待它。

你可以在deviceready之后调用你的jquery函数。让我给你一个完整的例子来说明如何做到这一点。 首先确保你已经添加了你将要使用的所需的cordova/phonegap插件。在我的例子中,我已经加载了设备插件

打开空白文档添加html框架,然后在html的头部添加以下内容:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>//include the cordova.js file
<script type="text/javascript" charset="utf-8" src="js/jquery.min.js"></script>//include the jquery.min.js file strictly after the cordova.js file
<script type="text/javascript" charset="utf-8">


    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {

        $(document).ready(function(){ //jquery starts here and is inside the device ready function


            //the next few lines are now the calling my plugins
            //#deviceproperties is a paragraph in html that has an id deviceproperties
            //#hideme is a span inside the paragaraph that has the id deviceproperties.inside the span is the statement that is saying 'loading device properties' and will be removed as soon as the script hide() runs

           $('#deviceProperties').prepend("<br>" + device.cordova 
                                 + "<br>" + device.platform
                                 + "<br>" + device.uuid
                                 + "<br>" + device.version
                                  );

            $('#hideme').hide();//hide the loading device properties
        });//lets end jquery`s document.ready

    }//lets end cordova`s device ready

</script>
//包括cordova.js文件
//将jquery.min.js文件严格放在cordova.js文件之后
//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
函数ondevicerady(){
$(document).ready(function(){//jquery从这里开始,位于设备就绪函数内
//接下来的几行是调用我的插件
//#deviceproperties是html中id为deviceproperties的段落
//#hideme是Paragraph中的一个span,它具有id deviceproperties。span中的语句表示“正在加载设备属性”,并将在脚本hide()运行后立即删除
$(“#设备属性”).prepend(“
”+device.cordova +“
”+device.platform +“
”+device.uuid +“
”+device.version ); $('#hideme').hide();//隐藏加载设备属性 });//让我们结束jquery的文档。就绪 }//让cordova的设备准备就绪

正在加载设备属性


您可能被否决了,因为尽管您是正确的,但问题是jQuery和cordova在库完全加载和初始化时都会触发“我准备好了”类型的事件,我认为触发这些事件的顺序不一定与它们的脚本标记出现的顺序相同(例如,如果第一个脚本执行大量ajax请求。)虽然,不是肯定的。用一个监听器更新了我的答案,检查准备好的事件。我假设OP需要监听两个事件监听器才能启动,但我不确定,这就是为什么我没有回答…但是你的扩展答案对我来说很好。我认为两个监听器都可以。是的,我相信我需要监听两个事件侦听器,因为我正在使用jQuery侦听
单击
事件,该事件将调用
cordova.exec
与我的插件通信。
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>//include the cordova.js file
<script type="text/javascript" charset="utf-8" src="js/jquery.min.js"></script>//include the jquery.min.js file strictly after the cordova.js file
<script type="text/javascript" charset="utf-8">


    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {

        $(document).ready(function(){ //jquery starts here and is inside the device ready function


            //the next few lines are now the calling my plugins
            //#deviceproperties is a paragraph in html that has an id deviceproperties
            //#hideme is a span inside the paragaraph that has the id deviceproperties.inside the span is the statement that is saying 'loading device properties' and will be removed as soon as the script hide() runs

           $('#deviceProperties').prepend("<br>" + device.cordova 
                                 + "<br>" + device.platform
                                 + "<br>" + device.uuid
                                 + "<br>" + device.version
                                  );

            $('#hideme').hide();//hide the loading device properties
        });//lets end jquery`s document.ready

    }//lets end cordova`s device ready

</script>