【问题标题】:jQuery's .getScript()... what am I doing wrong?jQuery 的 .getScript() ......我做错了什么?
【发布时间】:2012-01-12 23:45:11
【问题描述】:

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

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

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

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

【问题讨论】:

  • 设置警报或类似的东西来记录deviceready 事件触发,我经历了一段地狱般的时光,某些版本的PhoneGap 没有触发deviceready 事件(甚至尽管 API 可供使用)。
  • 是的,deviceready 正在触发。调用了deviceReady下的其他插件,就是不知道怎么调用.js文件。
  • “本地”是指file:/// 还是http://localhost/?使用 file: 时某些功能会受到限制,因此设置 HTTP 服务器可能会有所帮助。

标签: javascript jquery cordova getscript


【解决方案1】:

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

  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");
    }
    

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2017-09-10
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多