我网站中的一些页面有PWA安装功能( instagram in app浏览器不支持)。所以当我的访问者点击这些页面链接时,我想在本地浏览器中打开它,而不是在Instagram in-app浏览器中进行导航。
我尝试了以下几点:
$('.my-link').on('click', function(evt){
if(navigator.userAgent.includes("Instagram")){
evt.preventDefault();
window.open($(this).attr('href'), '_blank');
return false;
}
return true;
});
if条件匹配为true,但目标页面在Instagram in-app浏览器中仍然处于打开状态。我已经尝试过window.open($(this).attr('href')、‘_system’;也没有成功。
有线索吗?
更新1
试图使用URI方案强制打开浏览器,但不幸的是,它似乎是一个很好的解决方案。Safari没有URI方案。谷歌Chrome有,但我们不能保证用户在Android中有Chrome浏览器,我们也不能检测浏览器用户有什么浏览器。
$('a.new-window-if-instagram').on('click', function(evt){
if(navigator.userAgent.includes("Instagram")){
if (/android/i.test(navigator.userAgent)) {
evt.preventDefault();
var url = $(this).attr('href').replace("https://", "googlechromes://");
url = url.replace("http://", "googlechrome://");
window.open(url, '_blank');
return false;
}
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
// Safari doest have URI scheme...
}
}
return true;
});
发布于 2021-10-15 14:01:26
我目前也有同样的问题,但至少我找到了一个适用于Android的解决方案。我不知道您是否使用PHP作为后端语言,但是如果其他人发现这一点,这可能是有用的:
<?php
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if ((strpos($ua, 'mobile/') !== false) && (strpos($ua, 'safari/') == false)) // Validation code for iOS mobile
{
// Show a message that ask user to open with default browser
}
else if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) // Validation code for Android mobile
{
header('Content-type: application/pdf');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
}
这是对iOS应用程序内浏览器和Android浏览器的验证。
For Android:我们将页面的内容类型更改为。由于应用程序内浏览器无法处理PDF文件,它将在真正的浏览器中打开页面(用户设置中定义的默认浏览器)。当页面再次加载时,验证将失败,因为它不再是应用程序中的浏览器。
For iOS:由于我们可以在iOS中验证应用程序内的浏览器,但不能自动地重定向用户,我现在要做的是显示一条消息,要求用户使用默认浏览器打开这个页面(您可以在User-Agent
上用开关情况更改消息,因为这并不是每个应用程序内浏览器的相同步骤)。
如果我找到了iOS的有效解决方案,我将更新我的答案。
https://stackoverflow.com/questions/68258427
复制相似问题