Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make redirect to instagram://user?username={username}

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?

like image 781
user912830823 Avatar asked Sep 27 '22 11:09

user912830823


2 Answers

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

like image 137
Giuseppe Avatar answered Oct 13 '22 00:10

Giuseppe


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 !

like image 21
Nitin Bagoriya Avatar answered Oct 13 '22 00:10

Nitin Bagoriya