tags:

views:

137

answers:

2

What should I put instead of: http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike in this href field so that it returns the permalink of the webpage. I want to insert the same code in every webpage of the website.

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;amp;layout=standard&amp;amp;show_faces=true&amp;a
mp;  width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>
A: 

If, for instance, you were using ASP.NET with MasterPages, you'd put this just below the ContentPlaceHolder for your article content. Pass it some variables for the page's filename, the title of the article, etc.

Regardless of your templating model, you want the anchor to work out to something like this:


<a href="http://www.facebook.com/sharer.php?u=http://YOURPAGE.ASPX&t=TITLEOFYOURPAGE+-+YOURWEBSITENAME"&gt;&lt;img src="http://www.facebook.com/images/connect_favicon.png" border="0" alt="Share this article on Facebook" /></a>
Superstringcheese
A: 

It depends on what server-side language you're using. In PHP you could do something like this, where I reference $_SERVER['REQUEST_URI']:

<?php $encodedUrl = htmlentities(urlencode($_SERVER['REQUEST_URI'])); ?>
<iframe src="http://www.facebook.com/plugins/like.php?href=&lt;?php echo $encodedUrl; ?>&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>

If you're not using a server-side language, you could do it in JavaScript. This is untested, but:

<script type="text/javascript">
    var encodedUrl = escape(encodeURIComponent(window.location));
    document.write('<iframe src="http://www.facebook.com/plugins/like.php?href=' + encodedUrl + '&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>');
</script>
Matt Huggins
Both your examples are vulnerable to [cross-site scripting](http://en.wikipedia.org/wiki/Cross-site_scripting) attacks. The page location needs to be [URL encoded](http://en.wikipedia.org/wiki/Percent-encoding) and then HTML-escaped before being included as a parameter in the Facebook URL.
Phil Ross
Thanks for pointing that out, Phil. I updated the answer to encode the URL in each example.
Matt Huggins

related questions