How to put url dynamically for facebook comment boxes, specific to each page

0

In a response in php we can do 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 it could do to have dynamically each url of their pages, in Javascript, without having to put a comment box with their respective url on each page of the site?

    
asked by anonymous 15.06.2014 / 22:39

2 answers

0

After a few days trying to find the solution, the savior of our souls, Jesus Christ, directed me (it is not therefore to find the solution but to take the opportunity to praise it).

These codes are complete, by simply copying and pasting wherever you want the comment box to appear, it will dynamically render your domain and add a unique comment box on every page of your site. You may be changing the 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>
    
16.06.2014 / 23:14
1

You can pass as variable in res.render() . Home Within your likebox.jade, I would do something like:

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

You should 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:)

    
16.06.2014 / 01:28