Completely prevent iOS web app from opening link in mobile Safari, even with links containing parameters

耗尽温柔 提交于 2019-12-11 05:02:19

问题


I have found multiple solutions to prevent an iOS web app from opening normal links in mobile Safari, unfortunately they do not work for links containing parameters, e.g.

href="index.php?s=example"

These are still opened in mobile Safari.

The shortest solution for normal links I have found thus far can be found here at stackoverflow. Maybe someone can modify that script?


回答1:


Try 4 years old github gist. I used it and it works. You can use it by bower:

bower install --save iosweblinks

May be you have javascript errors on page and handlers do not call?

P.S. My modified script for prevent opening links in Safari in standalone mode:

(function (standalone) {

    if (!standalone) {
        return;
    }

    document.addEventListener('click', function (e) {
        var element = e.target,
            href = '';

        while (!/^(a|html)$/i.test(element.nodeName)) {
            element = element.parentNode;
        }

        if (element.getAttribute) {
            href = element.getAttribute('href');

            if ('' !== href && '#' !== href && null !== href && (!element.protocol || element.protocol !== 'tel:')) {
                e.preventDefault();
                window.location = element.href;
            }
        }
    }, false);

}(window.navigator.standalone));


来源:https://stackoverflow.com/questions/27529966/completely-prevent-ios-web-app-from-opening-link-in-mobile-safari-even-with-lin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!