Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook “Like box” in ajax loaded page only loads the first time?

Tags:

ajax

facebook

I want to show the Facebook "Like box" plugin and some other FB code in a ajax loaded page.

When I load it in a ordinary html page it works as it should, but when I try to put it in a ajax load page it only loads the first time, when I click on the link to load the page the second time it is not loading the page? It is only loading the page if I clear the cache.

The index file init the fb code.

FB.init({
  appId      : '277065222', // App ID
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true,  // parse XFBML
  oauth      : true
});

I load the ajax page with this code:

 function loadFacebook(){

$('#container').load('http://www.manmade.se/manmade/guiden/facebook_onweb.html');
FB.XFBML.parse();
}

And in the ajax loaded page I have the facebook code, eg the facebook Like box.

like image 958
Claes Gustavsson Avatar asked Apr 06 '12 08:04

Claes Gustavsson


2 Answers

you can call fb par script in your page load after calling fb 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/en_US/all.js#xfbml=1&appId=11111code";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


$(document).ajaxComplete(function () {
    try {
        FB.XFBML.parse();
    } catch (ex) { }
});
like image 140
bugrasitemkar Avatar answered Oct 19 '22 09:10

bugrasitemkar


reloadlike.html

<div class="fb-like fltLeft" data-href="http://yrl_like" data-send="false" data-layout="button_count" data-width="87" data-show-faces="false"></div>

this should work every time.

I guess your callback is not right.

$('#container').load('reloadlike.html', function() {
    FB.XFBML.parse();
});
like image 37
Madan Avatar answered Oct 19 '22 07:10

Madan