如何重定向到 Instagram://user?username={username} [英] How to make redirect to instagram://user?username={username}

查看:50
本文介绍了如何重定向到 Instagram://user?username={username}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 html 页面上有这个链接,可以在特定用户上打开 Instagram 应用程序:

i have this link on my html page that open Instagram app on specific user:

<a href="instagram://user?username={USERNAME}">Link to Instagram Profile</a>

我一直在寻找自动运行 url 'instagram://user?username={USERNAME}' 的方法(如自动重定向).

I was looking for the ways to run the url 'instagram://user?username={USERNAME}' automatically (like automatic redirect).

这是我在头脑中尝试过的:

Here is what i've tried in the head:

1.

<meta http-equiv="refresh" content="0; url=instagram://user?username={USERNAME}" />

2.

<script type="text/javascript">
window.location.href = "instagram://user?username={USERNAME}"
</script>

3.@Nitin-bagoriya 建议的变体

3. variation suggested by @Nitin-bagoriya

<script> function load() { window.location.href = "instagram://user?username={USERNAME}"}; window.onload = load; </script> 

  1. @Giuseppe 建议的变化

  1. variation suggested by @Giuseppe

addEventListener('load', load);

addEventListener('load', load);

但是当我浏览那里时 - 什么也没有发生.它已在安装了 Instagram 应用的 android 上进行了测试.

But when i browse there - nothing happen. It was tested on android with instagram app installed.

我想知道有没有办法通过重定向自动运行 Instagram 应用程序,这样用户就不需要点击链接?

I wonder is there any way to run Instagram app automatically via redirect so user dont need to click the link?

推荐答案

回顾,让他人受益.

移动操作系统(iOS 或 Android)不支持基于window.location.href"的 Javascript 解决方案.

Javascript solutions based in "window.location.href" are not supported in mobile OS (iOS or Android).

似乎首先起作用的是在 PHP 中添加重定向,如此处的回答,如下所示:

What seemed to first work is adding the redirect in PHP, as answered here, like this:

<?php header("Location: instagram://user?username={USERNAME}") ; ?>

2.Javascript 中的(更好)解决方案:

iOS 提供了window.location.replace"来进行重定向,而 Android 提供的是document.location".因此,您需要先进行设备检测,然后再应用正确的重定向.

2. (Better) solution in Javascript:

iOS provides "window.location.replace" to do a redirect, while what Android provides is "document.location". Thus, you need to device-detect first, and then apply the proper redirect.

var destination = "instagram://user?username={USERNAME}"; 
if( navigator.userAgent.match(/Android/i) ) {
  // use Android's redirect
  document.location = destination;   
}   
else {
  // use iOS redirect
  window.location.replace( destination );
}

请参阅此处了解详情:iOS 和 Android 重定向在javascript中

这篇关于如何重定向到 Instagram://user?username={username}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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