【问题标题】:How to handle links in Phonegap + JQM app?如何处理 Phonegap + JQM 应用程序中的链接?
【发布时间】:2015-01-16 10:24:16
【问题描述】:

我正在使用 Phonegap 和 jQuerymobile 构建一个应用程序。该应用程序大致是这样工作的:

1) 应用程序从公共服务器下载 ZIP 文件,然后将其解压缩到本地文件夹。我从fileSystem.root.toNativeURL() 获得了本地文件夹路径(在操作系统中,它是这样的:file://var/mobile/Container/Data/Application/xxxx/Documents/

2) 应用重定向到在本地文件夹中解压缩的 HTML(例如:file://var/mobile/Container/Data/Application/xxxx/Documents/index.html

我现在在 index.html 文件中遇到问题 b/c,所有链接都是绝对路径(例如:<a href="/content/index2.html">Link</a>)。这会破坏所有链接,因为(我假设)它们现在都指向 file://content/index2.html 而不是 file://var/mobile/Container/Data/Application/xxxx/Documents/content/index2.html

我的问题是,我应该如何处理链接?我在想我应该重写所有链接以强制在其前面添加本地文件夹 URL。有没有更好的办法?

如果重写链接是要走的路,我怎么能用 jQuerymobile 做到这一点?我在 jQuery 中做到了这一点,这似乎可以工作 http://jsfiddle.net/jg4ouqc5/ 但这段代码在我的应用程序中不起作用(jQueryMobile)

【问题讨论】:

    标签: javascript jquery cordova jquery-mobile


    【解决方案1】:

    当您加载 index.html 时,您将获得 file://some_path/..../index.html 作为您的基本 URL。现在自己遇到的任何链接都可以根据基本 URL 进行解析。

    你会更好地了解你的场景。可能有多种方法可以解决此问题。

    • 与 CMS/代码生成器签订合同。应始终生成相对于基本 URL 或绝对的链接。您在页面中获得的链接是错误的 - <a href="/content/index2.html">Link</a> 理想情况下应该是 <a href="content/index2.html">Link</a> 或完全限定的,如 https://www.google.com

    • 如果要更改 URL,则可以在解压缩内容后使用本机代码进行更改。这将是非常直接的。

    • 如果您想在浏览器中更改 URL,则必须保留基本 url,然后处理以下几件事:

      a. 绝对网址 - 在您的情况下,您只需检查 window.location.protocol,如果它以“http”开头,然后跳过它。

      b. 子目录

    这是我写的一个小例子: 注意:我没有尝试过此代码,您可能需要根据需要进行更改。

    $(document).ready(function(){
            var base_file_name = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
    
            //In index.html (persist this value in native)
            var baseUrl = window.location.href.replace(base_file_name, "");
    
            $("a").each(function () {
                this.href =  baseUrl + this.pathname;
                $(this).click(function (e) {
                    e.preventDefault();
                    alert(this.pathname);
                    window.location.href = this.href;
                });
            });
        });
    

    【讨论】:

    • 谢谢,我最终采用了与您类似的 javascript 方式。除了因为我在 JQM 上,我不得不将 document.ready... 更改为 document.on('pagecreate'... 和其他一些东西。其余的或多或少是相同的。我犹豫是否要去javascript 方式(感觉 hack-ish),但它似乎是我能想到的唯一方式......
    • 很高兴您能够实现它。 :) 快乐编码
    【解决方案2】:

    您链接的示例应该可以工作,请确保您正确设置了<base>,并且您使用了正确的字符串进行替换。

    【讨论】:

      【解决方案3】:

      是的,您必须在页面加载时对所有 URL 进行规范化。我现在无法使用 phonegap 进行测试,但您的 basePath 需要是以下之一:

      • 您在答案中描述的文件路径(不太可能)
      • window.location.origin(可选包括window.location.pathname

      代码:

      // mini dom ready - https://github.com/DesignByOnyx/mini-domready
      (function(e,t,n){var r="attachEvent",i="addEventListener",s="DOMContentLoaded";if(!t[i])i=t[r]?(s="onreadystatechange")&&r:"";e[n]=function(r){/in/.test(t.readyState)?!i?setTimeout(function(){e[n](r)},9):t[i](s,r,false):r()}})
      (window,document,"domReady");
      
      domReady(function () {
          var anchors = document.getElementsByTagName['a'],
              basePath = /* get your base path here, without a trailing slash */;
      
          Array.prototype.forEach.call(anchors, function( anchor ){
              anchor.setAttribute('href', basePath + anchor.getAttribute('href'));
          });
      });
      

      【讨论】:

        【解决方案4】:

        删除链接开头的正斜杠。

        href="content/index2.html">
        

        【讨论】:

        • 我不能,HTML 是由 CMS 生成的。另外,如果我使链接相对,我们如何处理更深的文件夹?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 2020-05-15
        • 2022-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多