简体   繁体   English

如何在AJAX之后更新Facebook / Twitter分享按钮URL?

[英]How to update Facebook/Twitter share button URL after AJAX?

So, I have a single page app that plays videos from our database. 因此,我有一个单页应用程序,可播放数据库中的视频。 The page loads on www.foo.com/player.php and I use PHP to set a video ID parameter to have a shareable URL. 该页面在www.foo.com/player.php上加载,我使用PHP设置视频ID参数以具有可共享的URL。 So the page loads, the share URL is inserted to both FB and Twitter as http://www.foo.com/player.php?id=1 and the buttons share the proper URL. 因此,页面加载完毕,共享URL作为http://www.foo.com/player.php?id=1插入到FB和Twitter,按钮共享了正确的URL。 I'm using the standard implementations of both buttons. 我正在使用两个按钮的标准实现。

The issue is, when the user changes the video, neither button updates the URL, it still shows http://www.foo.com/player.php?id=1 . 问题是,当用户更改视频时,两个按钮都没有更新URL,它仍然显示http://www.foo.com/player.php?id=1

I've tried to add this to my AJAX success function: 我试图将其添加到我的AJAX成功函数中:

$(".facebook-share-button").data("href", "http://www.foo.com/player.php?id=" + data.videoID);
$(".twitter-share-button").data("url", "http://www.foo.com/player.php?id=" + data.videoID);

If I check those data attributes in the console, they show the proper URL, so I know it's update the attributes with what I want, but the button functions don't pick up the new URL. 如果我在控制台中检查那些数据属性,它们会显示正确的URL,因此我知道它会使用所需的属性更新属性,但是按钮功能不会选择新的URL。

For Twitter, I found their suggestion to refresh the button with 对于Twitter,我发现他们的建议是使用

tweet_button.render();

But that doesn't update my shared URL. 但这不会更新我的共享URL。

I also tried updating the browser's URL with the history.pushstate, and it does update in the browser's display, but still isn't detected by the share buttons. 我还尝试使用history.pushstate更新浏览器的URL,并且它确实在浏览器的显示中进行更新,但是共享按钮仍未检测到。

Anyone dealt this this issue? 有人处理过这个问题吗?

Twitter and Facebook both create iframes when they render their share buttons, which means you cannot affect them directly. Twitter和Facebook在渲染共享按钮时都会创建iframe,这意味着您不能直接影响它们。 What you need to do is reload the buttons entirely, removing the previous ones and replacing them with new ones. 您需要做的是完全重新加载按钮,删除先前的按钮,然后将其替换为新的按钮。 Here's how I do that: 这是我的方法:

In your html file, you prepare to load the buttons: 在html文件中,您准备加载按钮:

<div id="fb-root"></div>
<script type="text/javascript">
    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)){
            return;
        }
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
 </script>
<span id='fbholder'></span>

<span id='twitterholder'></span>

Note that the elements that will be replaced by buttons are not there. 请注意,将不存在将由按钮替换的元素。 Also note that the external script for Twitter is not there. 还要注意,Twitter的外部脚本不存在。 We're going to load those in a function, like so: 我们将这些加载到一个函数中,如下所示:

function reloadSocialButtons() {
    try {
        // Twitter
        var t = $("<a href='https://twitter.com/share' class='twitter-share-button'>");
        var tjs = $('<script>').attr('src', '//platform.twitter.com/widgets.js');
        $('#twitterholder').empty();
        $(t).attr('data-text', document.title);
        $(t).attr('data-url', location.href);
        $(t).appendTo($('#twitterholder'));
        $(tjs).appendTo($('#twitterholder'));

        // Facebook
        var f = $('<div class="fb-share-button" data-layout="button">');
        $(f).attr('data-href', location.href);
        $('#fbholder').empty();
        $(f).appendTo($('#fbholder'));
        FB.XFBML.parse(document);
    } catch(ex){}
};

So what does that code do? 那该代码做什么? For Twitter, first we create a new share-button link. 对于Twitter,首先我们创建一个新的共享按钮链接。 This is what was absent from the html. 这就是html所没有的。 Then we create a new script tag to load the missing js file. 然后,我们创建一个新的脚本标签来加载丢失的js文件。 Next we empty anything that was already in our holder tag (the previous button). 接下来,我们清空Holder标记中的所有内容(上一个按钮)。 Then we update the text and url for our new share-button link, append it to our holder tag, and then append the script to the holder tag. 然后,我们为新的共享按钮链接更新文本和url,将其附加到我们的holder标签,然后将脚本附加到holder标签。 When the script is loaded, it automatically looks for any twitter share-button links and turns them into buttons, generating the new button with the correct link. 加载脚本后,它将自动查找任何twitter共享按钮链接,并将其转换为按钮,从而生成具有正确链接的新按钮。

The Facebook part is basically the same, except we didn't have to reload and append the script file. Facebook部分基本上是相同的,除了我们不必重新加载和附加脚本文件。 Instead we just call FB.XFBML.parse(document) to make the new Facebook share-button link turn into a button. 相反,我们只是调用FB.XFBML.parse(document)来使新的Facebook共享按钮链接变成一个按钮。 You should be able to do the same with Twitter, but apparently it's inconsistent, so reloading the script each time is a consistent solution. 您应该可以对Twitter进行相同的操作,但是显然这是不一致的,因此每次重新加载脚本都是一个一致的解决方案。

Now, whenever you want to update your share buttons, you just call the function, and let it do all the work. 现在,只要您想更新共享按钮,就可以调用该函数,然后让它完成所有工作。 If you want it to happen every time an ajax call completes, for instance in a single-page backbone.js environment, then instead of putting it in a function, you can put it in $(document).ajaxComplete(function() { ... }); 如果您希望每次ajax调用完成时都发生这种情况,例如在单页的ribs.js环境中,那么可以将其放入$(document).ajaxComplete(function() { ... });

I'm not sure how or why, but if you simply change the data-url and reload, it doesn't work. 我不确定如何或为什么,但是如果您只是更改data-url并重新加载,它将无法正常工作。 I tried replacing the entire html of the element with the anchor element and then ran twttr.widgets.load(); 我尝试用anchor元素替换元素的整个html,然后运行twttr.widgets.load(); after and it worked fine. 之后,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM