我在我的应用程序开始时初始化了推文按钮,在用户交互后,使用HTML5 pushState更新当前窗口的位置,但Twitter按钮仍然分享自上次初始化时的先前URL。
我该如何更新Twitter使用的URL?
我在我的应用程序开始时初始化了推文按钮,在用户交互后,使用HTML5 pushState更新当前窗口的位置,但Twitter按钮仍然分享自上次初始化时的先前URL。
我该如何更新Twitter使用的URL?
我使用了Twitter的新功能,成功地重新加载了分享URL。
function updateTwitterValues(share_url, title) {
// clear out the <a> tag that's currently there...probably don't really need this since you're replacing whatever is in there already.
$(container_div_name).html(' ');
$(container_div_name).html('<a href="https://twitter.com/share" class="twitter-share-button" data-url="' + share_url +'" data-size="large" data-text="' + title + '" data-count="none">Tweet</a>');
twttr.widgets.load();
}
<div id="twitter-share-section"></div>
我解决了这个问题。以下是使其正常工作的方法:
总体思路是:
class
设置为除 .twitter-share-button
以外的其他值。同时,给 <a>
添加一个样式 style="display:none;"
。id
的 <div>
(或者 <span>
)。data-url
等属性style
属性(取消隐藏)class
更改为 twitter-share-button
append()
此克隆到步骤 2 中的 <div>
中。$.getScript()
加载和运行 Twitter 的 JavaScript。这将把你克隆的 <a>
转换为带有所有正确设置的 <iframe>
。以下是我的 HTML 代码(使用 Jade):
a.twitter-share-button-template(style="display:none;", href='https://twitter.com/share=', data-lang='en',
data-url='http://urlthatwillbe_dynamically_replaced',
data-via='ckindel', data-text='Check this out!',
data-counturl='http://counturlthatwillbe_dynamically_replaced') Share with Twitter
#twitter-share-button-div
然后在你的客户端 .js 中:
// the twitter share button does not natively support being dynamically
// updated after the page loads. We want to give it a unique (social_id)
// link whenver this modal is shown. We engage in some jQuery fun here to
// clone a 'hidden' twitter share button and then force the Twitter
// Javascript to load.
// remove any previous clone
$('#twitter-share-button-div').empty()
// create a clone of the twitter share button template
var clone = $('.twitter-share-button-template').clone()
// fix up our clone
clone.removeAttr("style"); // unhide the clone
clone.attr("data-url", someDynamicUrl);
clone.attr("data-counturl", someDynamicCountUrl);
clone.attr("class", "twitter-share-button");
// copy cloned button into div that we can clear later
$('#twitter-share-button-div').append(clone);
// reload twitter scripts to force them to run, converting a to iframe
$.getScript("http://platform.twitter.com/widgets.js");
我从这里获得了解决方案的主要线索: http://tumblr.christophercamps.com/post/4072517874/dynamic-tweet-button-text-using-jquery
twttr.widgets.load();
来自Twitter开发文档。
<body>
The tweet button:
<span id='tweet-span'>
<a href="https://twitter.com/share" id='tweet'
class="twitter-share-button"
data-lang="en"
data-count='none'>Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</span>
<div>
<input type="button" onclick="tweetSetup('http://www.google.com','MyGoogle')" value="TestTwitter" />
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function tweetSetup(dynamicurl,dynamictext) {
$(".twitter-share-button").remove();
var tweet = $('<a>')
.attr('href', "https://twitter.com/share")
.attr('id', "tweet")
.attr('class', "twitter-share-button")
.attr('data-lang', "en")
.attr('data-count', "none")
.text('Tweet');
$("#tweet-span").prepend(tweet);
tweet.attr('data-text', dynamictext);//add dynamic title if need
tweet.attr('data-url', dynamicurl);
twttr.widgets.load();
}
</script>
</body>
data-url
属性:$('.twitter-share-button').attr("data-url", "http://mynewlink");
但 Twitter 的 JavaScript 已经运行,这不起作用。 求助! - tig