javascriptflashfacebook-javascript-sdkfacebook-social-plugins

How to dynamically change facebook comments plugin url based on javascript variable?


I want to dynamically change the data-href for the fb comments plugin below based on a javascript variable. I'm running a flash swf file and am passing the new link for data-href into the html wrapper via a javascript function. When I do that, I want the fb comments plugin to refresh to the new data-href link.

<div style="float: left; padding-left:5px; min-height:500px" class="fb-comments" data-href="www.example.com" data-num-posts="20" data-width="380"></div>

Called javascript function passing in the new link for the comments plugin:

function changeCommentsUrl(newUrl){
// should refresh fb comments plugin for the "newUrl" variable
}

Solution

  • This will load the initial comments box, the script will when executed will clear the comments div wrapper and replace html5 comments box with new url. JS SDK will then parse the new box.

    JS SDK is required for this work. refer to https://developers.facebook.com/docs/reference/javascript/

    fix for xfbml render from dom manipulation


    <div id="comments">
    <div style="float: left; padding-left:5px; min-height:500px" class="fb-comments" data-href="www.example.com" data-num-posts="20" data-width="380"></div>
    </div>
    <script>
    function changeCommentsUrl(newUrl){
    // should refresh fb comments plugin for the "newUrl" variable
    document.getElementById('comments').innerHTML='';
    parser=document.getElementById('comments');
    parser.innerHTML='<div style="float: left; padding-left:5px; min-height:500px" class="fb-comments" data-href="'+newUrl+'" data-num-posts="20" data-width="380"></div>';
    FB.XFBML.parse(parser);
    }
    </script>
    

    user solved:

    document.getElementById('comments').innerHTML='<div style="float: left; padding-left:5px; min-height:500px" class="fb-comments" data-href="'+link+'" data-num-posts="20" data-width="380"></div>'; 
    FB.XFBML.parse(document.getElementById('comments'));