Dharma Records

A record of Ānandajoti’s publication work.

Menu
  • Notices
  • Talks
  • Texts
  • Audio
  • Video
  • Photos
  • Ecology
  • Culture
  • Archives
  • About
Menu

Facebook Comments: Dynamic Urls on Static Pages

Posted on September 18, 2011March 28, 2013 by Ānandajoti

Javascript

If you are interested in using the Facebook commenting system you can get the code and set the main parameters for your plugin from the following Facebook developer’s page. Basically they all work on the same principles, but the design and url will be different, of course.

To set it up, first of all in the <head> section you need to include your Facebook Id, and the application Id (get one from here), just before the closing </head> tag:

 
<meta property="fb:admins" content="YourFacebookId"/ >
<meta property="fb:app_id" content="YourApplicationId"/>
 

My Facebook Id, for example, is “anandajoti” and the application ID (which I can’t share) is a 15-digit number.

Then it is necessary to write the code into the page at the place where you want the Comments Box to appear. If you want to insert the html via javascript so you don’t have to update each and every page, the code looks like this:

 
document.write('<div id="fb-root"></div>');
document.write('<script>(function(d){ var js, id = "facebook-jssdk"; if (d.getElementById(id)) {return;} js = d.createElement("script"); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; d.getElementsByTagName("head")[0].appendChild(js)(document));</script>');
document.write('<fb:comments href="https://www.mywebsite.com" num_posts="10" width="500"></fb:comments>');
 

which effectively inserts the following to all the pages:

 
<div id="fb-root"></div>
<script>(function(d){ var js, id = "facebook-jssdk"; if (d.getElementById(id)) {return;} js = d.createElement("script"); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; d.getElementsByTagName("head")[0].appendChild(js);}(document));</script>
<fb:comments href="https://www.mywebsite.com" num_posts="10" width="500"></fb:comments>
 

However, the problem then was that commenting on one page inserted the comment site-wide, i.e. all the pages showed the same comment, and they all linked back to the Home Page, which is not what was needed, of course. This is because the href is set at the Home Page in the coding (https://www.mywebsite.com, on line 3 above).

So how to make the address dynamic? There are a few solutions out there, but more than half involved php, which you may not be using; and some others seem to be using out-of-date Facebook code, which no longer applies.

I did see one solution which said leave out the address, and it will automatically be filled in with the current one, but these days, at least, if you leave out the address it returns an error.

In the end – though the end was a long time coming – it was quite easy: give the href an Id, empty the href, and afterwards write in the address of the current page dynamically by finding out its location.

I am using jquery to write the address in here, but there are other ways. The trick is in the last line below:

 
<div id="fb-root"></div>
<script>(function(d){ var js, id = "facebook-jssdk"; if (d.getElementById(id)) {return;} js = d.createElement("script"); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; d.getElementsByTagName("head")[0].appendChild(js);}(document));</script>
<fb:comments id="FC" href="" num_posts="10" width="500"></fb:comments>
<script type="text/javascript" language="javascript">jQuery(“#FC”).attr(“href”, window.location.href.split(‘?’)[0]);</script>');
 

Here is the javascript code;

 
document.write('<div id="fb-root"></div>');
document.write('<script>(function(d){ var js, id = "facebook-jssdk"; if (d.getElementById(id)) {return;} js = d.createElement("script"); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; d.getElementsByTagName("head")[0].appendChild(js);}(document));</script>');
document.write('<fb:comments id="FC" href="" num_posts="10" width="500"></fb:comments>');
document.write('<script type="text/javascript" language="javascript">jQuery(“#FC”).attr(“href”, window.location.href.split(‘?’)[0]);</script>');
 

Why the Facebook development team don’t offer such a solution is best known to them, but I hope this will help someone else who is facing the same problem.

20 thoughts on “Facebook Comments: Dynamic Urls on Static Pages”

  1. Timothy says:
    December 2, 2011 at 9:40 pm

    Thank you sooo soo much for your script. When i needed to embed a comment box for my dynamic product pages i searched for long beforen i got you. Later on I realised that when someone clicked on a shared url, they were taken to a page with no previous comments! (the comment box you gave cannot detach the url query-string so every comment has its own unique url! ) The solution took me some time to figure out (Im new to javascript) but finally, its here: replace the last line with: jQuery(“#FC”).attr(“href”, window.location.href.split(‘?’)[0]);

  2. Anandajoti says:
    December 3, 2011 at 2:22 pm

    Dear Timothy, many thanks for finding that bug, and also for providing the solution. I have updated the code to suit.

  3. Timothy says:
    December 3, 2011 at 8:45 pm

    You are welcome. I have the code running perfectly on vhdmedia.com pages – amazon astore products. But I am still struggling with making the comment box retrieve the product image to be shared as thumbnail over facebook. How would you deal with this issue?

  4. Anandajoti says:
    December 5, 2011 at 6:26 am

    Hi Timothy, Facebook should retrieve the thumbnail automatically, this is bound up with their coding (I believe) not mine. If anyone has a solution then it would be good to hear.

  5. Karl says:
    December 18, 2011 at 5:22 pm

    Could you elaborate on how /where to apply the code you’ve offered us less-enabled readers?

  6. Anandajoti says:
    December 18, 2011 at 5:28 pm

    Dear Karl, you put the top code (including the id etc.) in the head section of the htm page, and the rest of the code is inserted wherever you want the comments to appear (normally near the bottom of the page).

  7. Karl says:
    December 18, 2011 at 6:39 pm

    Dear Anan,

    Thank you kindly for your advice. I am that far with it – but where I am stuck is the javascript.. I’ve no idea what or where to put it.

    I presume the last FB code (above JS) will call the javascript. So do I just create a file with the JS code and save it to my root?

    I thank you for your speedy reply to my previous question.

    Regards

  8. Anandajoti says:
    December 19, 2011 at 6:13 am

    Dear Karl, it depends on how the website is set up. If you have a non-cms website (ie not wordpress, drupal or something similar) you just put the javascript into a separate file, call it ‘facebookcommentbox.js’ or something similar, and then insert it with the code pointing to the location of your file.

    If you are using cms it would need to be inserted through your template, which differs according to whatever you are using. Best then to look it up with the cms themselves, on their forum or whatever.

  9. Karl says:
    December 19, 2011 at 8:58 pm

    Thank you Anan, much obliged for your assistance & advice. 🙂 I now have it at the bottom of my SHOWTHREAD template 🙂

  10. Timothy says:
    February 10, 2012 at 12:12 am

    Hi Anan, I have designed a text decorator for use on social networks. Since the text is generated to a text box and should be shared on social sites, i need to enable mobile phone users to copy-paste the decorated text into a social comment box by just clicking one button. The challenge is that the fb comment box, or disqus and others are offered in iframes. Can someone figure out the correction for my button code:

    input value=”Encool” type=”button” class=”button” style=”width:70%”
    onclick=”
    window.frames[‘iframename’].document.getElementById(‘formname’).textareaname.value =
    document.getElementById(‘decoratedtextarea’).value;
    “

  11. Sumit Dobriyal says:
    March 30, 2012 at 7:48 pm

    Dear

    when i try to apply the code for my dynamic url i.e http://abc.com/page.aspx?id=1 then the following error occurs in facebook comment box …

    The comments plugin requires an href parameter.

  12. Artur says:
    March 31, 2012 at 1:20 am

    HI Anan,
    thanks for the helpful post. Could you try to figure out also a Pin button URL for dymanic page and image. Here’s the sample code i generated in Pinterest Goodies website:

    <a href="http://pinterest.com/pin/create/button/?url=www.example.com%2FdynamicImage&amp;media=www.exampleL.com%2FdynamicURL" rel="nofollow"></a>
    

    I assume it can be a similar solution as a Facebook comments, using jQuery – but i don’t know JavaScript
    Thanks, have a good day.
    Artur

  13. Mike says:
    May 21, 2012 at 2:27 pm

    Hi all,
    I have the same problem like Sumit Dobriyal.
    Any toughts ?

  14. Anandajoti says:
    May 21, 2012 at 2:32 pm

    Hi Mike, Sumit, all. I am very sorry I experimented with Facebook comments on my site for a while, but it didn’t work out, and I moved to Disqus. Therefore it is difficult to offer advice on this anymore, as I do not have a site to check through. I will leave the comments here open though in case someone else can help with this.

  15. The InfoStrides says:
    August 4, 2012 at 7:38 pm

    Thanks for the tutorials but unfortunately I received this error.

    Warning: this comments plugin is operating in compatibility mode, but has no posts yet. Consider specifying an explicit ‘href’ as suggested in the comments plugin documentation to take advantage of all plugin features.

  16. Jobs says:
    August 4, 2012 at 7:40 pm

    Nice tutorial but do not work for me as espected.

    href error returned because I wanted dynamic url.

  17. Leo Maverick says:
    September 29, 2012 at 4:37 am

    thanks for this info, im about to upgrade my website to allow facebook comments and before implement it was my first question… how to support dinamyc url??
    my website is http://www.amazoniabeats.com/
    thanks!!

  18. EURO-SPACE.net says:
    February 18, 2013 at 12:16 am

    Easily achievable with php:

    <?PHP
    $currentlink = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    echo "”;
    ?>

  19. Alexis says:
    March 27, 2013 at 10:29 pm

    Hola, he tratado incansablemente de activar este sistema, pero hasta ahora no he tenido resultados…
    me sigue dando el siguiente mensaje:
    El plugin de comentarios requiere un parámetro href.
    así esta el código en la pagina:

    (function(d){ var js, id = “facebook-jssdk”; if (d.getElementById(id)) {return;} js = d.createElement(“script”); js.id = id; js.async = true; js.src = “//connect.facebook.net/en_US/all.js#xfbml=1”; d.getElementsByTagName(“head”)[0].appendChild(js);}(document));

    jQuery(“#FC”).attr(“href”, window.location.href.split(‘?’)[0]);’);

  20. Anandajoti says:
    March 28, 2013 at 7:51 am

    As this page is way out of date I am closing the comments on it now.

Comments are closed.

QR code

SHORTLINK

Donations

there are many expenses involved in the making of this website. Even a small donation really helps to maintain and expand the site.

Recent Posts

  • Dhammanīti, Dhamma Wisdom Published
  • Childers’ On Sandhi in Pali Published
  • Checkboxes for Long Works on Ancient Buddhist Texts
  • Dharma Storybooks on Photo Dharma
  • The Exalted Chapters, A Simplified Translation of the Udānavarga
  • Translation of the Sanskrit Udānavarga Published
  • The Lives of Inspiring Chinese Nuns
  • Five New Albums on Photo Dharma
  • Henri van Zeyst’s Contributions to the Sri Lankan Encyclopedia of Buddhism
  • The Reliefs of Buddhavanam eBook Published

Top Ten Tags

Buddha

Dhamma

Sangha

India

China

Indonesia

Thailand

Temples

Wisdom

Ethics

Other Websites

for my other websites please see my
LINKTREE

Sponsorship

 hosting sponsored by exabytes.my 

Random Posts

  • Republication of Daily Chanting
  • The Commemorations on the July Full Moon Day
  • The Divine Traditions (Divyavadana) reliefs at Borobudur
  • BWV 001: One should be Amenable to Admonition
  • BWV 049: Comparing Oneself with Others
  • The Silk Road Transmission of Buddhism
  • Chanting at the International Buddhist College
  • New Navigation Menus on my Three Main Sites
  • Bhante Anandajoti: Generosity (Dāna)
  • Bhikkhu Gavesako: Life as a Buddhist Monk in the West

Recent Posts

  • Dhammanīti, Dhamma Wisdom Published
  • Childers’ On Sandhi in Pali Published
  • Checkboxes for Long Works on Ancient Buddhist Texts
  • Dharma Storybooks on Photo Dharma
  • The Exalted Chapters, A Simplified Translation of the Udānavarga
  • Translation of the Sanskrit Udānavarga Published
  • The Lives of Inspiring Chinese Nuns
  • Five New Albums on Photo Dharma
  • Henri van Zeyst’s Contributions to the Sri Lankan Encyclopedia of Buddhism
  • The Reliefs of Buddhavanam eBook Published

Related Posts:

  • Writing Filenames into mp3 Tags with a Batch File
  • Resize images to Fit Browser Window
  • Writing Filenames into JPEG Comments and Captions…
  • MS Word: Split Windows Vertically
  • Making short urls using javascript and a .htaccess file
  • Facebook Profile to Page Conversion, My Experience
  • New Website: Dharma Documentaries
  • Early Buddhist Legends of Sri Lanka
  • The Background to the Buddha's Teaching
  • Proposal: Free Publishing Platform for Dharma Workers
© 2026 Dharma Records | Powered by Minimalist Blog WordPress Theme