最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make redirect to instagram:user?username={username} - Stack Overflow

programmeradmin0浏览0评论

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>

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. variation suggested by @Nitin-bagoriya

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

    addEventListener('load', load);

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

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

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>

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. variation suggested by @Nitin-bagoriya

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

    addEventListener('load', load);

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

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

Share Improve this question edited Oct 17, 2015 at 13:02 user912830823 asked Oct 17, 2015 at 8:53 user912830823user912830823 1,3523 gold badges16 silver badges27 bronze badges 3
  • Later you will ux-check whether instagram is defined or not, won't you? In dev time, you could alert something in the case instagram is not defined, so you know it flows as you expect. – Giuseppe Commented Oct 17, 2015 at 12:06
  • @Giuseppe, sorry not sure what defined mean, but only users with instagram app installed can land on this page. – user912830823 Commented Oct 17, 2015 at 12:39
  • Then it's unnecessary. Perfect :) – Giuseppe Commented Oct 17, 2015 at 12:41
Add a ment  | 

2 Answers 2

Reset to default 2

Recaping, so others can benefit.

0. Main problem:

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

1. Solution in 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. (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 );
}

See here for details: iOS and Android redirects in javascript

You Just need to add window.onLoad in head section of .html file

Your Final code will be like:

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

OR you can create a function which contain your window.location.href = "instagram://user?username={USERNAME}" and execute it on window.onload !

发布评论

评论列表(0)

  1. 暂无评论