简体   繁体   English

添加这在ajax加载后无法正常工作

[英]AddThis not working after ajax load

I have the AddThis js for bookmarking the details to the calender.This is working correctly on page load,but I do some filtering using ajax load and replace the html, after this the AddThis button not showing.here is my code for ajax . 我有AddThis js用于将详细信息加入日历。这在页面加载时正常工作,但是我使用ajax加载进行了一些过滤并替换了html,之后AddThis按钮没有显示。这是我的ajax代码。

$('document').ready(function () {

    $('.eventSelect').change(function () {
       var selectedDate = $('#eventDate').val();
        var keyword = $('#eventsearch').val();
        var url = "/EventsHome?eventDate=" + selectedDate + "&keyword=" + keyword;
       $.ajax({
           type: "GET"
          , url: url
          , success: function (data) {
              console.log($(data).find(".eventList").html());
              $(".eventList").html($(data).find(".eventList").html());
             var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
                  if (window.addthis) {
                      window.addthis = null;
                      window._adr = null;
                      window._atc = null;
                      window._atd = null;
                      window._ate = null;
                      window._atr = null;
                      window._atw = null;
                  }
                  $.getScript(script);

             }
          , error: function (XMLHttpRequest, textStatus, errorThrown) {

          }, comeplete: 

Use this script when you load new content: 加载新内容时使用此脚本:

if(typeof addthis !== 'undefined') { addthis.layers.refresh(); }

It is only solution for addthis_inline_share_toolbox ! 它只是addthis_inline_share_toolbox的解决方案!

addthis.toolbox() works only when you have specified buttons inside addthis element (mostly older versions). addthis.toolbox()仅当你在addthis元素中指定了按钮(大多数是旧版本)时才有效。

After 1 hour of trying custom codes i found this one that works perfect. 在尝试自定义代码1小时后,我发现这个完美无缺。

$(document).ajaxStop(function() {
  if (window.addthis) {
    window.addthis = null;
    window._adr = null;
    window._atc = null;
    window._atd = null;
    window._ate = null;
    window._atr = null;
    window._atw = null;
  }
  return $.getScript("http://s7.addthis.com/js/300/addthis_widget.js#pubid=sdive");
});

This is the source 这是来源

You shouldn't need to reset the AddThis variables and reload the script, if you simply call: 如果只是调用,则不需要重置AddThis变量并重新加载脚本:

addthis.toolbox();

this should re-render the buttons according to whatever configuration you have specified. 这应该根据您指定的任何配置重新呈现按钮。 Take a look at the documentation here: 看一下这里的文档:

http://support.addthis.com/customer/portal/articles/1293805-using-addthis-asynchronously#.UvvWw0JdWTM http://support.addthis.com/customer/portal/articles/1293805-using-addthis-asynchronously#.UvvWw0JdWTM

Would have added this as a comment to Sol's answer , but I lack the rep. 本来会把这个作为对Sol的答案的评论,但我缺乏代表。 The actual syntax for reloading the toolbox is to pass in the class selector for the toolbox, however this assumes that you have already init'ed addthis 重新加载工具箱的实际语法是传入工具箱的类选择器,但这假设您已经初始化了addthis

 addthis.toolbox('.addthis_toolbox')

You can use live method. 您可以使用实时方法。

With jQuery 1.4.2 $('.eventSelect').live('change', function(){ works with FF, Safari, Opera, but not for IE 使用jQuery 1.4.2 $('。eventSelect')。live('change',function(){适用于FF,Safari,Opera,但不适用于IE

Gary's answer did it for me: https://stackoverflow.com/a/21176470/3656694 Gary的回答为我做了: https//stackoverflow.com/a/21176470/3656694

Then running addthis.toolbox('.addthis_toolbox') re-initialized addthis for me. 然后运行addthis.toolbox('.addthis_toolbox')为我重新初始化addthis。

Hope this helps someone! 希望这有助于某人! :) :)

In order to work with ajax load and/or multiple addthis instances on the same page you need to insert the elements inside the div: 要在同一页面上使用ajax加载和/或多个addthis实例,您需要在div中插入元素:

<div class="addthis_toolbox" data-url="domain.com" data-title="title">
    <a class="addthis_button_facebook" style="cursor:pointer"></a> 
    <a class="addthis_button_twitter" style="cursor:pointer"></a> 
    <a class="addthis_button_email" style="cursor:pointer"></a>
</div>

and then after your ajax is done run addthis.toolbox('.addthis_toolbox') 然后在你的ajax完成之后运行addthis.toolbox('.addthis_toolbox')

If you are using the AddThis share buttons, you might have noticed that once you initialize it on page load, it does not really work for content that is loaded via ajax after the fact. 如果您使用的是AddThis共享按钮,您可能已经注意到,一旦在页面加载时初始化它,它就不适用于事后通过ajax加载的内容。

ie AddThis is unaware of fresly loaded content. 即AddThis不知道fresly加载的内容。 The fix for this is pretty simple. 对此的修复非常简单。 Just ask AddThis to refresh as follows, and it will automatically regenerate the correct share links for newly loaded content 只需按以下方式询问AddThis进行刷新,它将自动为新加载的内容重新生成正确的共享链接

addthis.layers.refresh(); addthis.layers.refresh();

I realized that my adblocker was responsible for 我意识到我的广告拦截器负责

if(typeof addthis !== 'undefined') { addthis.layers.refresh(); }

not working. 不工作 (addthis.layers.refresh was undefined.) This is strange since I have the exact same code on 4 sites, but only on the one it doesn't work. (addthis.layers.refresh未定义。)这很奇怪,因为我在4个站点上有完全相同的代码,但只在它不起作用的那个上。 So if you are having problems, try turning off your adblocker to see if anything changes. 因此,如果您遇到问题,请尝试关闭广告拦截器以查看是否有任何更改。

The code Alex posted worked for me when I had my adblocker turned on (uBlock origin). 当我打开adblocker(uBlock origin)时,Alex发布的代码为我工作。 I decided to run it as follows, so that I do it the "right" way and only if it won't work, then run the alternative way. 我决定按如下方式运行它,以便我以“正确”的方式执行它,并且只有它不起作用,然后运行替代方式。

if (typeof addthis !== 'undefined') {
    if (typeof addthis.layers.refresh !== 'undefined' && $.isFunction(addthis.layers.refresh)) {
        addthis.layers.refresh();
    } else {
        if (window.addthis) {
            window.addthis = null;
            window._adr = null;
            window._atc = null;
            window._atd = null;
            window._ate = null;
            window._atr = null;
            window._atw = null;
        }
        return $.getScript("http://s7.addthis.com/js/300/YOURLINK");
    }
}

i resolve this! 我解决了这个! Link of Documentation 文档链接

 addthis_share.title = 'title of shared object'; addthis_share.url = 'URL to share'; addthis_share.description = 'description of shared'; addthis_share.swfurl ='URL of a Flash object to share, along with the link'; addthis_share.width ='ideal width of any provided Flash object'; addthis_share.height ='ideal height of any provide Flash object'; addthis_share.email_template ='name of template to use for emailed shares '; addthis_share.email_vars ='associative array mapping custom email variables to values '; 
 <script type="text/javascript"> var addthis_share = addthis_share||{ url_transforms : { clean: true } }; </script> <script type="text/javascript" src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4e9c308d658c185a&async=1"></script> 

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

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