jQuery 的 .getScript()...我做错了什么?

发布于 2024-12-26 10:49:18 字数 461 浏览 2 评论 0原文

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

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

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

我研究了又研究,但就是不明白为什么我的“order.js”文件没有被 $.getScript 调用方法。有什么想法吗?或者是否有其他方法可以在我的 index.html 的 deviceReady 函数中调用此 .js 文件?

I'm programming an iPhone app with Phonegap. I have local .html and .js files. The following is in my index.html file:

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

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

I researched and researched, but just can't figure out why my "order.js" file isn't getting called by the $.getScript method. Any ideas? Or is there any other way to call this .js file within the deviceReady function in my index.html?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2025-01-02 10:49:18

对我来说,以下解决方案非常有效。

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

    函数 init()
    {
        // 创建基于ajax的自定义缓存脚本导入器
        jQuery.cachedScript = 函数(url, 选项)
        {
            // 允许自定义选项,但 dataType、cache 和 url 始终是预定义的
            选项 = $.extend(选项 || {},
            {
                数据类型:“脚本”,
                缓存:真,
                网址: 网址
            });
            返回 jQuery.ajax(选项);
        };
    
        导入脚本();
    }
    
  2. 导入脚本并可选择处理 donefail:< /p>

    函数 importScripts()
    {
        $.cachedScript("js/events.js")
            // 等待脚本加载,然后添加监听器
            .done(函数()
            {
                document.addEventListener("deviceready", onDeviceReady, false);
            });
        $.cachedScript("js/navigation.js");
        $.cachedScript("js/mark.js");
    }
    

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

For me the following solution worked very well.

  1. Add a custom jQuery function that uses ajax and caches the loaded script:

    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. Import the scripts and optionally handle done and fail:

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

Thats it : )
More information may be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文