4

I needed to get a unique url for each page and adapted a script that I found through the help of other members here.

One is for the like button and the other is for the comment box. Is there a more elegant way to do this without so much code?

<br>

<!-- start facebook like code-->
<div id="thelike"></div>
<script>
var thisurl = document.URL;
function changeCommentsUrl(newUrl){
// should refresh fb like plugin for the "newUrl" variable
document.getElementById('thelike').innerHTML='';
parser=document.getElementById('thelike');
parser.innerHTML='<div class="fb-like" data-href="'+newUrl+'" data-send="true" data-layout="button_count" data-width="400" data-show-faces="false"></div>';
FB.XFBML.parse(parser);
}
changeCommentsUrl(thisurl);
</script>
<!-- end facebook like code-->

<br>

<!-- start facebook comment code-->
<div id="thecomments"></div>
<script>
var thisurl = document.URL;
function changeCommentsUrl(newUrl){
// should refresh fb comments plugin for the "newUrl" variable
document.getElementById('thecomments').innerHTML='';
parser=document.getElementById('thecomments');
parser.innerHTML='<div class="fb-comments" data-href="'+newUrl+'" data-num-posts="10" data-width="400"></div>';
FB.XFBML.parse(parser);
}
changeCommentsUrl(thisurl);
</script>
<!-- end facebook coment code-->

<br>
ShawnDaGeek
  • 4,145
  • 1
  • 22
  • 39
Mick Ronson
  • 41
  • 1
  • 1
  • 5
  • Your script looks ok for what you are trying to do, the only thing i think i would chance is combining them into one function. Using 2 functions may better suite your needs for this though. – ShawnDaGeek Oct 09 '12 at 16:17

1 Answers1

0

If you don't put in a URL when generating the code, it will default to the current page if that's what you are trying to achieve.

imp
  • 460
  • 2
  • 7
  • 23