【发布时间】:2014-10-27 21:24:55
【问题描述】:
当用户打开网站时,我希望发生以下两种情况之一:
- 如果安装了应用程序 mycoolapp,则使用自定义 url 方案 mycoolapp:// 将用户重定向到应用程序
- 如果应用程序未安装,则停留在当前页面上,不会发布或重定向到未知页面或弹出错误消息。
案例1很简单,可以使用如下代码:
window.location = "mycoolapp://";
如果应用程序安装在设备上,这将起作用。当应用程序未安装时,它会将用户重定向到未知的空白页面。
对于案例 2,我有一个使用 iFrame 的解决方案,该解决方案在 iOS 和原生 Android 浏览器上运行良好。它不适用于 Android 版 Chrome。
var redirect = function (location) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', location);
iframe.setAttribute('width', '1px');
iframe.setAttribute('height', '1px');
iframe.setAttribute('position', 'absolute');
iframe.setAttribute('top', '0');
iframe.setAttribute('left', '0');
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
};
redirect('mycoolapp://');
安装该应用后,它可以在原生 Android 浏览器上运行,但在 Android 版 Chrome 上没有重定向。页面上什么也没有发生。
如何在未安装应用时重定向到我在 Chrome for Android 上运行的应用而不重定向到空白页面?
编辑:我知道你可以使用 Intent
window.location = "intent://#Intent;package=com.mycoolapp;scheme=mycoolapp;launchFlags=268435456;end;";
这不是我想要的,因为如果未安装应用程序,它会在 google play 上启动应用程序页面。有没有办法不重定向到google play?
【问题讨论】:
-
在 android 上不要使用“特殊”方案...在 Intent 过滤器中使用 http 方案...浏览器会要求用户使用浏览器或您的应用程序(如果已安装)完成操作跨度>
-
@Selvin 你能回答你的想法吗?
-
和RGraham的回答一样
标签: javascript android html redirect