Tuesday, February 14, 2012

change the url in facebook comments code?


on my website I have more than 200 pages and I want to add facebook comments feature to my website. I ran to this problem is that I have to change the code (the url) for each page. I do not want to change the code for every page. If you can see the second piece of code contain Url goes here can I include a method like location.href so that it can use the current url of the page. can I do this ? I do not care if it is php, javascript, or anything ealse.



Include the JavaScript SDK on your page once, ideally right after the opening <body> tag.




<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/en_US/all.js#xfbml=1&appId=324524777119";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>



Place the code for your plugin wherever you want the plugin to appear on your page.




<div class="fb-comments" data-href="Url Goes Here" data-num-posts="2" data-width="470"></div>

1 comment:

  1. You can use div and then dynamically create its contents with the innerHTML method in JavaScript
    where it will be your fb-comments tag. You can get the current page with document.location.href.

    After you create the fb-comments tag dynamically and render it inside the div, you need to reparse its contents so the XFBML is interpreted. You can do it with the FB.XFBML.parse() method.

    var mydiv = document.getElementById('myCommentsDiv');
    mydiv.innerHTML =
    '<div class="fb-comments" data-href="' + document.location.href + '" data-num-posts="2" data-width="470"></fb:comments>';
    FB.XFBML.parse(mydiv);

    ReplyDelete