文章演示了网页中如何分享到Facebook和Twitter中,并且可各自携带hashtag。
目前,Facebook分享参数中仅可传递一个tag,Twitter参数中可传递多个tag。
Story:
Twitter参数传递tag非常简单,其支持多个直接在url参数多加入tag标签
shareurl + '&hashtags=mazumamobile,refurbishediphone,iphone,refurbished,apple,renew,iphonexs'
Facebook参数传递tag则是一波三折,无奈继续尝试!
- 最开始尝试用和Twitter一样的方式, 失败
- 然后google了一下,stackoverflow中的回答说Facebook更改了隐私策略,已不再支持上述格式,推荐用SDK的方式。
stackoverflow问题地址
但是用SDK一直报一个错误。
Can't Load URL The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.- 最后发现facebook是支持在url中带tag参数的,但是需要url编码,如下:
解决方案地址
shareurl + '%23hashtags=mazumamobile'
tag问题终于解决
最后分享一下我的源码!
Html代码如下:
<div class="share-friends-button-continer">
<a class="cart-share-btn" title="Facebook" href="javascript:openShareFacebookWindow()">
<img src="img/FACEBOOK-MOBILE.png" alt="FACEBOOK" />
</a>
<a class="cart-share-btn" title="Twitter" href="javascript:openShareTwitterWindow()">
<img src="img/TWITTER-MOBILE.png" alt="TWITTER" />
</a>
</div>
JS代码如下:
<script>
function openShareFacebookWindow() {
var shareUrl = "http://www.facebook.com/sharer.php?u=" + $("#share-url").val() + "&hashtag=%23refurbishediphone"; //注意这里#要编码为%23才可被识别
var winWidth = 520;
var winHeight = 400;
var winTop = (screen.height / 2) - (winHeight / 2);
var winLeft = (screen.width / 2) - (winWidth / 2);
window.open(shareUrl, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width=' + winWidth + ',height=' + winHeight);
}
function openShareTwitterWindow() {
var shareUrl = "http://twitter.com/share?url=" + $("#share-url").val() + "&hashtags=mazumamobile,refurbishediphone,iphone,refurbished,apple,renew,iphonexs";
var winWidth = 520;
var winHeight = 400;
var winTop = (screen.height / 2) - (winHeight / 2);
var winLeft = (screen.width / 2) - (winWidth / 2);
window.open(shareUrl, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width=' + winWidth + ',height=' + winHeight);
}
</script>
结束!
网友评论