我希望有可能是检测URI是否某种方式:方案注册的移动设备上从浏览器中。
IE:我想检查Facebook,微博,Pinterest的应用程序的安装,并且可以从它们相关的URI推出:方案。
if(fb_isInstalled) {
// href="fb://profile/...."
} else {
// href="http://m.facebook.com/..."
}
基本上,如果用户已经安装了Facebook的,然后启动应用程序,但如果不安装该应用程序回落到FB网站的移动版本。
我想我已经得到了有效的解决方案。
<!-- links will work as expected where javascript is disabled-->
<a class="intent"
href="http://facebook.com/someProfile"
data-scheme="fb://profile/10000">facebook</a>
而我的JavaScript是这样工作的。
注意:有一个小的jQuery混在里面,但你并不需要使用它,如果你不想。
(function () {
// tries to execute the uri:scheme
function goToUri(uri, href) {
var start, end, elapsed;
// start a timer
start = new Date().getTime();
// attempt to redirect to the uri:scheme
// the lovely thing about javascript is that it's single threadded.
// if this WORKS, it'll stutter for a split second, causing the timer to be off
document.location = uri;
// end timer
end = new Date().getTime();
elapsed = (end - start);
// if there's no elapsed time, then the scheme didn't fire, and we head to the url.
if (elapsed < 1) {
document.location = href;
}
}
$('a.intent').on('click', function (event) {
goToUri($(this).data('scheme'), $(this).attr('href'));
event.preventDefault();
});
})();
我也扔这件事作为一个要点 ,你可以派生和乱用。 您还可以在要点中的jsfiddle,如果你选择。
编辑
@kmallea分叉主旨和从根本上简化了。 https://gist.github.com/kmallea/6784568
// tries to execute the uri:scheme
function uriSchemeWithHyperlinkFallback(uri, href) {
if(!window.open(uri)){
window.location = href;
}
}
// `intent` is the class we're using to wire this up. Use whatever you like.
$('a.intent').on('click', function (event) {
uriSchemeWithHyperlinkFallback($(this).data('scheme'), $(this).attr('href'));
// we don't want the default browser behavior kicking in and screwing everything up.
event.preventDefault();
});
文章来源: How to launch apps (facebook/twitter/etc) from mobile browser but fall back to hyperlink if the app isn't installed