4

我正在用 Phonegap 编写一个 iPhone 应用程序。我有本地.html.js文件。以下是在我的index.html文件中:

function onBodyLoad() {
     document.addEventListener("deviceready", deviceReady, false);
}

function deviceReady() {
     $.getScript("js/order.js");
}

我进行了研究和研究,但无法弄清楚为什么我的“order.js”文件没有被该$.getScript方法调用。有任何想法吗?或者有没有其他方法可以.js在我的 deviceReady 函数中调用这个文件index.html

4

1 回答 1

0

对我来说,以下解决方案效果很好。

  1. 添加一个使用 ajax 并缓存加载脚本的自定义 jQuery 函数:

    function init()
    {
        // Create a custom cached script importer based on ajax
        jQuery.cachedScript = function(url, options)
        {
            // Allow custom options, but dataType, cache and url are always predefined
            options = $.extend(options || {},
            {
                dataType: "script",
                cache: true,
                url: url
            });
            return jQuery.ajax(options);
        };
    
        importScripts();
    }
    
  2. 导入脚本并可选择处理donefail

    function importScripts()
    {
        $.cachedScript("js/events.js")
            // Wait for the script to be loaded, before adding the listener
            .done(function()
            {
                document.addEventListener("deviceready", onDeviceReady, false);
            });
        $.cachedScript("js/navigation.js");
        $.cachedScript("js/mark.js");
    }
    

就是这样 :) 更多信息可以在这里找到。

于 2014-03-19T15:23:56.327 回答