问题描述
<a class="grey-text text-lighten-3" target="_blank" href="javascript:void(0);" onclick="document.write(yourOS())">Instagram</a>
function yourOS() {
var ua = navigator.platform.toLowerCase();
if (ua.indexOf("android") != -1) {
document.write(.link("instagram://user?username=owendunnigan"));
} else {
.link("http://www.instagram.com/OwenDunnigan");
}
}
我正在尝试将应用程序深层链接到我的网站,但我不知道如何将其转到计算机上的常规旧Instagram,然后转到Android或iOS设备上的应用程序。我知道链接有效,因为我分别尝试了它们。
I am trying to deep link apps into my website, but I can't figure out how to have it go to regular old Instagram on the computer, but then go to the app on Android or iOS device. I know that the links work because I tried them out individually.
推荐答案
我将以不同的方式来处理。而不是onclick检查源,我将执行此onload。因此:
I would approach this in a different manner. Instead of onclick checking the source, I would do this onload. So:
<script>
function onLoad(){
var urlLink = "http://www.instagram.com/OwenDunnigan";
var urlLink2 = "http://www.twitter.com/OwenDunnigan";
var urlLink3 = "http://www.facebook.com/OwenDunnigan";
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) {
urlLink = "instagram://user?username=owendunnigan";
urlLink = "twitter://user?username=owendunnigan";
urlLink = "facebook://user?username=owendunnigan";
}
document.getElementById('yourLink').setAttribute('href', urlLink);
document.getElementById('yourLink2').setAttribute('href', urlLink2);
document.getElementById('yourLink3').setAttribute('href', urlLink3);
}
window.onload = onLoad;
</script>
<a id="yourLink" class="grey-text text-lighten-3" target="_blank">Link</a>
我在我的android设备/笔记本电脑上对此进行了测试,并且对两者均有效。
I tested this on my android device / laptop and it worked for both.
编辑:肮脏的方式。您可以通过具有onload函数的函数来调用传递变量来制作更具有OOP风格的样式,但是为了简单起见,这可以解决问题。
Down and dirty way. You could make a more OOP style by having a function for the onload function to call passing in the variables, but for simplicity, this does the trick.
Edit2:
<script>
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
if( userAgent.match( /iPad/i ) || userAgent.match( /iPhone/i ) || userAgent.match( /iPod/i ) )
{
return 'iOS';
}
else if( userAgent.match( /Android/i ) )
{
return 'Android';
}
else
{
return 'unknown';
}
}
function onLoad(){
var urlLink1 = "http://www.instagram.com/OwenDunnigan";
var urlLink2 = "http://www.twitter.com/OwenDunnigan";
var urlLink3 = "http://www.facebook.com/OwenDunnigan";
switch(getMobileOperatingSystem()){
case 'Android':
urlLink1 = "instagram://user?username=owendunnigan";
urlLink2 = "twitter://user?username=owendunnigan";
urlLink3 = "facebook://user?username=owendunnigan";
break;
case 'iOS':
urlLink1 = "instagram://user?username=owendunnigan";
urlLink2 = "twitter://user?username=owendunnigan";
urlLink3 = "facebook://user?username=owendunnigan";
break;
default:
break;
}
document.getElementById('yourLink1').setAttribute('href', urlLink1);
document.getElementById('yourLink2').setAttribute('href', urlLink2);
document.getElementById('yourLink3').setAttribute('href', urlLink3);
}
window.onload = onLoad;
</script>
<a id="yourLink1" class="grey-text text-lighten-3" target="_blank">Link1</a>
<a id="yourLink2" class="grey-text text-lighten-3" target="_blank">Link2</a>
<a id="yourLink3" class="grey-text text-lighten-3" target="_blank">Link3</a>
清理代码并从此处滚动功能:
您可以通过简单地扩展detect函数并添加case语句来添加其他系统。当然,我没有添加通常情况下的默认case语句,但是我首先调用了变量,因此我认为没有理由重置它们,但是您可以做很多事情。让我知道这是否行不通。
Cleaned up the code and rolled in the function from here: Detecting iOS / Android Operating systemYou can add other systems by simply expanding the detect function and adding case statements. Granted, I didn't add a default case statement which you normally do, but I invoked the variables first, so no reason to reset them in my opinion, but you could do a lot more with it. Let me know if this doesn't work.
这篇关于如何在网站中进行深层链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!