ios - Phonegap + jQuery 移动 : injecting html files from iOS app's Documents folder with jQuery Mobile

标签 ios ajax cordova jquery-mobile

我正在开发一个同时使用 Phonegap 和 JQuery Mobile 的应用程序。
该应用程序连接到外部服务器以检查新的内容/内容更新(html 和 pdf 文件)。
如果需要,在 iOS 中,这些文件会成功下载到 app/Documents 文件夹中。
然后应用程序检索每个内容文件的绝对路径(file://localhost/var/mobile/Applications/APPID/Documents/subfolder)并创建链接到每个文件的绝对路径的 ListView 。

我遇到的问题如下:点击 ListView 会打开链接页面,但不是作为 ajax 调用。页面加载,但页面中没有引用 javascript(cordova.js、jquery.js app.js 等),因此我无法导航回主菜单。当我在/Documents 文件夹中打开 html 文件时,jQueryMobile ajax 导航似乎停止工作。
这只发生在/Documents 文件夹中的下载内容(因此在 Phonegap 的 www 文件夹之外)。


从远程调试器中,如果我尝试调用 $.mobile.changePage('previousPage.html')*函数,控制台返回 $ 未定义,好像页面无法引用 jQuery。但是在/www 文件夹中的所有页面中,我都不需要重新引用 js 文件。

该应用程序使用多页布局,每个页面在 <div data-role="page" id="pageid"> 之后都有自己的 javascript容器。
/Documents 文件夹中的每个 .html 都构造为一个 jQueryMobile 页面(具有数据角色属性)。

这是创建 ListView 的代码:

<script type="text/javascript">
    $('#content').live('pagebeforeshow', function(e) {
                            var curCat = parseInt(window.localStorage.getItem('currentCat'));

                            console.log('PAGE BEFORE SHOW: CONTENT');
                            db.db.transaction(function(tx) {
                                tx.executeSql('SELECT * FROM `content` WHERE `content`.`catID` = ?', [curCat],
                                    function(tx, result) {
                                        var path = window.localStorage.getItem('contentPath') + '/';
                                        if (result.rows && result.rows.length) {
                                            var html = '<ul data-role="listview" id="linkUl">';
                                            for (var i = 0; i < result.rows.length; i++) {
                                                var filename = result.rows.item(i).index_file.substr(result.rows.item(i).index_file.lastIndexOf('/'));
                                                console.log(result.rows.item(i).id);
                                                html += '<li id="' + result.rows.item(i).id + '">';
                                                html += '<a href="' + path + filename +'">';
                                                html += result.rows.item(i).title;
                                                html += '</a></li>';
                                            }
                                            html += '</ul>';
                                            console.log(html);
                                            $('#contCnt').html(html);
                                            $('ul#linkUl').listview();
                                        }

                                    },
                                    function(tx, error) {
                                        console.log(error.code);
                                        var html = '<div id="errorDB">';
                                            html += 'ERROR RETRIEVING FILES';
                                            html += '</div>';
                                        $('#contCnt').html(html);
                                    }
                                );
                            });
                        });
                        $('div#content').live('pageshow', function(e) {
                            $('ul#linkUl').listview('refresh');
                        });

</script>

其中 ContentPath 变量存储为 fileSystem 对象的 directory.toUrl();

我担心 jQueryMobile 无法从外部目录(iOS 中的/Documents 文件夹)中 ajax-pull html,或者我缺少一些属性或设置以便这样做。
也许是因为我使用的是绝对网址?如果是这样,我怎样才能从 Phonegap 的/www 文件夹中获取相对 url?
我是否必须在 cordova.plist 文件中声明某些内容?

此外,下载的内容不必包含任何 js,它们应该只是纯 html,但我需要在所有页面中保留 jQuery Mobile 页眉/页脚和导航系统。

我正在使用 Cordova 2.2.0 以及 jQuery 和 jQuery mobile 的最新版本。

提前感谢,如果格式出现问题,我很抱歉,我是 SO 新手(以防我尽快编辑)。

最佳答案

您可以从文档文件夹(或应用程序具有读取权限的任何其他文件夹)打开文件。

链接可能无法作为 ajax 页面加载的原因有两个

  • jQuery Mobile 认为它不是应用程序的一部分
  • 加载页面时出现 javascript 错误,这会导致运行默认链接单击操作而不是 ajax 加载程序。

  • 您可以尝试使用对 $.mobile.changePage 的调用,而不仅仅是设置链接 - 这让您对正在发生的事情有更多的了解。

    我认为不同文件夹中的文件 url 不应该被 jQuery Mobile 视为不同的域,但是为了消除这种可能性,构建文档文件夹的相对 url 应该是相当容易的。

    关于ios - Phonegap + jQuery 移动 : injecting html files from iOS app's Documents folder with jQuery Mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750432/

    相关文章:

    javascript - 如何使用依赖下拉列表在ajax中发送数据

    php - 图像上传到mysql数据库不起作用

    cordova - Google Analytics for Cordova 是否仍然有效?

    ios - 关闭 Cordova iOS 应用程序时,安全 cookie 会丢失

    ios - 应用程序显示在 iphone App Store 但未显示在 ipad AppStore

    objective-c - 确定 3G 与边缘

    Ios Swift 添加带有过渡/效果的 UIImageView

    javascript - 使用 javascript/jquery 增量读取 http 数据流

    javascript - 未捕获的类型错误 : Cannot read property 'querySelector' of null

    ios - 通过 Xcode 中的源代码管理将 Storyboard 提交到 SVN