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

后端 未结 2 627
谎友^
谎友^ 2021-01-18 08:06

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 da

相关标签:
2条回答
  • 2021-01-18 08:38

    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'));
    

    0 讨论(0)
  • 2021-01-18 08:41

    I found the simplest and effective way to make your Facebook Comment box to recognize the individual URL of each page (particularly good for e-commerce sites).

    Add this script to your top header portion of your website template (it generates de data-href value for your Comment Box div:

    <script type="text/javascript" language="javascript">
      jQuery("#FC").attr("data-href", window.location.href.split("?")[0]); 
    </script>
    

    And then on your Comment Box div, add the id for the value generated on the javascript:

    <div id="FC" class="fb-comments" data-href="" data-width="700" data-numposts="5" data-colorscheme="light">
    

    Voilá. I dedicated so much time to crack this nut, I just had to share it for you to save some time for break! Cheers!

    0 讨论(0)
提交回复
热议问题