Facebook plugin not working after ajax page load

I have a problem displaying a Facebook page on my website. I am using the new page plugin here .

The problem is that the plugin is only shown on the first page load. If I go to another page and come back, the plugin stops working. I am using ajax to navigate a website and to load new content into pages.

I tried to put javascript code from facebook on each page separately but didn't work. Also tried to make the plugin a function and call it $(document).ready

as well with content.load(this.href,myFunction());

but didn't work.

It doesn't matter, it will only show up on first boot.

This is the code I'm using:

index.php

<body>
<div id="fb-root"></div>
<script>
    (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/pt_BR/sdk.js#xfbml=1&version=v2.3&appId=289243507832799";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

[...rest of code...]
<div id="site"></div>
</body>

      

home.php

<div class="col-md-6">
    <div class="fb_social">
        <div class="fb-page" data-width="500" data-href="https://www.facebook.com/mypage" data-hide-cover="false" data-show-facepile="true" data-show-posts="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="https://www.facebook.com/mypage"><a href="https://www.facebook.com/mypage">MyPage</a></blockquote></div></div>
    </div>
</div>

      

contact.php

<div class="col-lg-12">
    <div class="fb_social">
        <div class="fb-page" data-width="500" data-href="https://www.facebook.com/mypage" data-hide-cover="false" data-show-facepile="true" data-show-posts="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="https://www.facebook.com/mypage"><a href="https://www.facebook.com/mypage">MyPage</a></blockquote></div></div>
    </div>
</div>

      

Jquery / Ajax to load content

$(document).ready(function() {
    var content = $('#site'),
        firstLink = $(".navbar li:first-child a"),
        firstLoad = firstLink.attr("href"),
        navLink = $(".navbar li a");

    //default load
    content.load(firstLoad);    

    navLink.on("click", function(event){
        event.preventDefault();
        event.stopImmediatePropagation();

        $(".active").removeClass("active");
        $(this).addClass("active");
        content.load(this.href);

    });
});

      

Does anyone know how to fix this? Thanks guys.

+3


source to share


1 answer


You need to use FB.XFBML.parse right after loading the content to parse the Social Plugin:



content.load(this.href, function () {
    FB.XFBML.parse();
});

      

+7


source







All Articles