How to place url dynamically to facebook comment boxes, specific to each page

Asked

Viewed 525 times

0

Lí in a reply that in php we can do like this:

<div class="fb-comments" data-href="<?php echo $url; ?>" data-width="687" data-numposts="7" data-colorscheme="light"></div>

  <?php
  $url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  ?>

However, I use Node.js and jade template engine, with jade template engine we can put in the "master page" a "block" and inside the block the content that will appear on all other pages, as could do to have dynamically each url of the respective pages, in Javascript, without having to put a comment box with their respective url on each page of the site?

2 answers

1

You can pass as variable in res.render().
Inside your Ikebox.jade, I’d file something like:

div(class="fb-comments" data-href="#{minhaurl}" data-width="687" data-numposts="7" data-colorscheme="light")

where you must compile as

    res.render('/rota', 'minhaurl: req.protocol + '://' + req.get('host') + req.originalUrl;'); 
// aqui a gente passa a minhaurl

You can find documentation on the req. used above: req.originalUrl and req.Protocol

I hope it helps :)

  • Just copy and paste the res.render in app.js ? I appreciate this north, I will try to find the solution via express, I had forgotten the express.

  • So the res.render has to do with your route right, you have to configure the functions you want to pull what you want and etc, but the scheme is to pass the variable 'minhaurl' q will give good :D

  • I appreciate the availability to help, friend, but I already found the solution via traditional javascript. It works exactly as wanted.

0


After a few days trying to find the solution the savior of our souls, Jesus Christ, directed me ( not so much to find the solution but rather to take the opportunity to praise him).

These codes are complete, by simply copying and pasting where you want the comment box to appear, it will dynamically render your domain and add a unique comment box to every page of your website. May be changing size, color etc ...

  <div id="thecomments"></div>
  <script>
     var thisurl = document.URL;
     function changeCommentsUrl(newUrl){
     // O plugin de comentarios facebook está dentro de parse.innerHTML 
     // e aparece no navegador dentro da div com id "thecomments"
     //e o data-href precisa estar com a variável " newUrl "
     document.getElementById('thecomments').innerHTML='';
     parser=document.getElementById('thecomments');
     parser.innerHTML='<div class="fb-comments" data-href="'+newUrl+'" data-width="687" data-numposts="7" data-colorscheme="light"></div>';
     FB.XFBML.parse(parser);
     }
     changeCommentsUrl(thisurl);
  </script>

  <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/pt_BR/sdk.js#xfbml=1&version=v2.0";
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  </script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.