问题描述
我正在执行在 Twitter 上分享"功能.我有分享按钮,当我点击它时会打开一个 twitter 小部件并显示我的分享框.当我按下小部件的 Tweet 按钮时,它会在我的推特时间线上发布我的消息.
I am doing the "share on Twitter" functionality. I have share button and when I click it will open a widget of twitter and showing me the share box. When I will press on Tweet button of widget , it will post my message on my twitter timeline.
现在我想处理一个回调,当我成功推文时,弹出窗口不应该显示,我的网站应该重定向下一页.
now I want to handle a callback, when I tweet successfully, the pop up should not display and my website should redirect next page.
我推荐了 Twitter 的推文按钮有回调吗? 这个问题,但不符合我的要求.
I refereed Is there a callback for Twitter's Tweet Button? this question but not working as per my requirements.
Twitter 提供事件,但它会在嵌入我们网站的 Tweet 按钮上执行.我尝试使用以下代码:
Twitter providing the events but it will execute on Tweet button which are embedded in our site. My tried with following code:
<a href="https://twitter/share" class="twitter-share-button" data-url="http://www.pinceladasdaweb.br/blog/" data-via="pinceladasdaweb" data-lang="pt" data-text="Pinceladas da Web">Tweetar</a>
js代码:
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="https://platform.twitter/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));
twttr.ready(function (twttr) {
twttr.events.bind('tweet', function () {
alert('Tweeted!');
});
});
});
推文成功后有没有办法重定向到下一页?
Is there a way to redirect to next page after successful tweet?
推荐答案
通常没有从推文回调的方法或意图.我使用以下 hack 在成功推文后进行回调:
Generally there is no method or intent for a callback from a tweet. I used below hack to make a callback after a successful tweet:
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="//platform.twitter/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));
$(document).on('click','.twitter',function(){
var tweetUrl = 'https://twitter/intent/tweet?url='+window.location.href;
var x = screen.width/2 - 700/2;
var y = screen.height/2 - 450/2;
var child = window.open(tweetUrl, "popupWindow", "width=600, height=400,left="+x+",top="+y);
child.focus();
var timer = setInterval(checkChild, 1);
document.domain = 'example'; //Replace it with your domain
function checkChild() {
if(child.closed){
clearInterval(timer);
}
if(child.window && child.window.closed){ //Code in this condition will execute after successfull post
//Do your stuff here
console.log('I am here after a successful tweet.');
clearInterval(timer);
}
}
});
<a class="twitter" href="javascript:void(0)">Tweet</a>
这篇关于推文成功后,执行回调+推特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论