【问题标题】:How to make redirect to instagram://user?username={username}如何重定向到 instagram://user?username={username}
【发布时间】:2016-01-16 01:06:18
【问题描述】:

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

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

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

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

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 建议的变化

<script> function load() { window.location.href = "instagram://user?username={USERNAME}"}; window.onload = load; </script> 
  1. @Giuseppe 建议的变体

    addEventListener('load', load);

但是当我在那里浏览时 - 什么也没有发生。 它在安装了 instagram 应用程序的 Android 上进行了测试。

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

【问题讨论】:

  • 稍后您将使用 ux-检查是否定义了 instagram,不是吗?在开发时,您可以在未定义 instagram 的情况下发出警报,因此您知道它会按预期流动。
  • @Giuseppe,抱歉不确定定义的含义,但只有安装了 instagram 应用程序的用户才能登陆此页面。
  • 那就没必要了。完美:)

标签: javascript android html url-redirection http-redirect


【解决方案1】:

回顾,让其他人受益。

0。主要问题:

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

1。 PHP中的解决方案:

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

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

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

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

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 and Android redirects in javascript

【讨论】:

    【解决方案2】:

    您只需在 .html 文件的 head 部分添加 window.onLoad

    您的最终代码如下:

    window.onLoad = window.location.href = "instagram://user?username={USERNAME}"

    或者您可以创建一个包含您的 window.location.href = "instagram://user?username={USERNAME}" 的函数并在 window.onload 上执行它强>!

    【讨论】:

    • 嗨@Nitin-bagoriya,感谢您的帮助。窗口没有重新加载。我已经使用了您的代码以及我在&lt;head&gt; &lt;html&gt; &lt;head&gt; &lt;script&gt; function load() { window.location.href = "instagram://user?username={username}; } window.onload = load; &lt;/script&gt; 之后直接放置的代码
    • 复制并粘贴以下代码。它会工作:) 1. 您在 elemtseva 后添加了分号,而在它之后缺少 "
    • 谢谢,嗯仍然不起作用,当我执行 window.onload = load 时 - 没有重定向。当我使用 window.onload = load() - 只是白屏..
    • 我刚刚使用了你的代码,没有发生重定向。当我添加 'http://' - 有重定向...
    • 如果有效,那么恭喜 :) 您的 url 中有错误,它不包含有效的 url btw
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2019-09-28
    相关资源
    最近更新 更多