【问题标题】:iPhone WebApp opens links in safariiPhone WebApp 在 safari 中打开链接
【发布时间】:2012-02-11 20:38:18
【问题描述】:

当我将“网络应用”添加到 iPhone 的主屏幕,然后打开它并单击一个链接时,它会打开 Safari。

我为我的问题找到了几个解决方案,但它们似乎不起作用:

iPhone Safari Web App opens links in new window

$('a').live('click', function (event)
{      
var href = $(this).attr("href");

if (href.indexOf(location.hostname) > -1)
{
    event.preventDefault();
    window.location = href;
}
});

http://jakeboyles.com/2011/01/16/how-to-build-an-iphone-and-ipad-web-app/

<a ontouchstart="window.location=yourlink.html' ">Your Link</a>

这两个帖子/教程都是在 iOS5 之前编写的。有新方法吗?我做错了吗?

感谢您的帮助

【问题讨论】:

标签: javascript iphone mobile-safari web-applications


【解决方案1】:

一个想法是让您的主屏幕应用程序成为 iframe,并让所有锚点都以它为目标。无需 javascript。

或者:

$('a').on('click touchend', function(ev){

    $(this).preventDefault();
    window.location = $(this).attr('href');

});

切勿将“touchstart”用于链接之类的内容。您只想检测用户何时停止触摸链接。检测按键、鼠标点击等也是如此。它是您要检测的事件的结束

.live 已在最新版本的 jQuery 中被弃用,因此从现在开始使用 .on(),它可以无缝地包裹现存和非现存元素。

【讨论】:

    【解决方案2】:

    此 javascript 代码适用于 iOS 5(它适用于我):

    在head标签中:

    <script type="text/javascript">
    function OpenLink(theLink){
        window.location.href = theLink.href;
    }
    </script>
    

    在你想在同一个窗口中打开的链接中:

    <a href="(your website here)" onclick="OpenLink(this); return false"> Link </a>
    

    代码基于此评论:http://mobile.tutsplus.com/tutorials/iphone/iphone-web-app-meta-tags/comment-page-1/#comment-10699

    【讨论】:

      【解决方案3】:

      这在 iPhone 5 上运行良好

      $(document).ready(function(){
      
          // Stop the default behavior of the browser, which
          // is to change the URL of the page.
          $("a").click(function(event){
              event.preventDefault();
              // Manually change the location of the page to stay in
              // "Standalone" mode and change the URL at the same time.   
              window.location = $(this).attr('href');
          });
       });
      

      记得链接到最新版本的jQuery。

      【讨论】:

        【解决方案4】:

        我是您所指文章的作者。我仍然使用我的 javascript 方法,它对我来说很好。 touchstart 适用于 Web 应用程序,因为浏览器需要更长的时间来呈现它。如果您打算制作一个原生应用程序,请不要使用它,但我已经为许多应用程序使用了 touchstart,它工作正常。

        谢谢 杰克博伊尔斯

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-22
          • 1970-01-01
          • 1970-01-01
          • 2011-06-01
          • 2011-02-23
          • 2015-11-25
          • 1970-01-01
          • 2023-03-10
          相关资源
          最近更新 更多