防止离线iPhone WebApp在Safari中打开链接 [英] Prevent offline iphone webapp from opening link in Safari

查看:142
本文介绍了防止离线iPhone WebApp在Safari中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,该网站可以在离线模式下与移动Safari一起使用.我可以将其收藏到主屏幕并从那里加载它.但是,一旦从主屏幕打开后,单击某些链接将跳出应用程序并在移动浏览器中打开-尽管事实上我在所有链接点击上都阻止了Default()!

I’m developing a website that will work with mobile safari in offline mode. I'm able to bookmark it to the home screen and load it from there. But, once opened from the home screen, clicking on certain links will jump out of the app and open in mobile safari – despite the fact that I preventDefault() on all link clicks!

应用程序在<body>级别绑定onclick事件处理程序.使用事件委托,它可以捕获对任何链接的任何单击,查看其href(例如"help"或"review"),并动态调用javascript模板并更新页面.事件处理程序在事件对象上调用preventDefault()–对于可以正常工作的链接中的 some ,页面将使用模板输出进行更新.但是,对于在输出模板结果之前导致本地数据库命中的链接,这些链接在移动safari中打开.

The app binds an onclick event handler at the <body> level. Using event delegation, it catches any click on any link, looks at its href (eg 'help' or 'review'), and dynamically calls a javascript template and update the pages. The event handler calls preventDefault() on the event object – for some of the links this works, and the page is updated with the template output. However, for the links that result in a hit against the local database before outputting the results of the template, the links are opened in mobile safari.

在桌面浏览器中,即使我处于离线状态,所有链接都可以使用-发生了与移动浏览器相关的事情.

In desktop safari, all the links work even when i’m offline – something is happening that’s mobile safari specific.

有没有想到为什么某些链接可以脱机工作,而另一些则不能?清单文件中未列出任何相关的链接URL,但是由于阻止了链接操作,因此不需要(不需要).

Any thoughts on why some links would work offline, but not others? None of the link URLs in question are listed in the manifest file, but they don’t (shouldn't) need to be since the link action is prevented.

几个额外的奇怪之处: *一旦我单击了一个加载到移动safari中的链接,即使我处于脱机状态,这些链接现在也可以使用,并且填充有数据库数据的模板也可以正常使用.换句话说:从主屏幕打开链接时失败,但从移动Safari浏览器离线中打开链接时失败 *更改链接以删除数据库命中(使用模拟数据库结果填充模板)可以解决问题,并且可以在主屏幕中的应用程序中单击链接.

a couple extra oddities: * once I click on a a link that loads in mobile safari, even if I'm offline, those same links now work, and the templates populated with data from the db work properly. in other words: the links fail when opened from the home screen, but not from within mobile safari offline * changing the link to remove the database hit (populating the template with a mock db result) solves the problem, and the links can be clicked in the app from the home screen.

推荐答案

您可能需要看一下: https://gist.github.com/1042026

// by https://github.com/irae
(function(document,navigator,standalone) {
    // prevents links from apps from oppening in mobile safari
    // this javascript must be the first script in your <head>
    if ((standalone in navigator) && navigator[standalone]) {
        var curnode, location=document.location, stop=/^(a|html)$/i;
        document.addEventListener('click', function(e) {
            curnode=e.target;
            while (!(stop).test(curnode.nodeName)) {
                curnode=curnode.parentNode;
            }
            // Condidions to do this only on links to your own app
            // if you want all links, use if('href' in curnode) instead.
            if('href' in curnode && ( curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host) ) ) {
                e.preventDefault();
                location.href = curnode.href;
            }
        },false);
    }
})(document,window.navigator,'standalone');

这篇关于防止离线iPhone WebApp在Safari中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆