Mobile app version of vmapp.org
Login or Join
Jessie594

: How do i make script, that detects its URL and displays it? I have a facebook like button on my website, but the data-href attribute needs a link to like: <div class="fb-like share-button

@Jessie594

Posted in: #Facebook #Html5

I have a facebook like button on my website, but the data-href attribute needs a link to like:

<div class="fb-like share-button fbbutton" data-href="htp://www.example.com/article/1.html" data-send="false" data-layout="button_count" data-width="450" data-show-faces="true"></div>


I have a lot of pages, and would like to copy the code on all my pages, and the code would automatically detect the url it's in. Example: data-href="$link$" so that i don't need to change the link to that of my pages.

Question: How do i make script, that detects its URL and displays it?

note: Website is written in html5

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Jessie594

3 Comments

Sorted by latest first Latest Oldest Best

 

@Cofer257

According to the Facebook developers guide, the Like button defaults to the current URL. So all you should need to do is remove the data-href attribute and it the same code will always use whatever page the user is on.

On the above page, just leave the URL box blank then click the "Get Code" button and follow the instructions.

10% popularity Vote Up Vote Down


 

@Welton855

Normally, I would suggest this be done server-side instead of by scripting, as suggested by @ionFish .

If you want to do this on the client-side, then you can simply use the Javascript Location object. The all you need is to invoke window.location to get the URL.

In the simplest form you just need:

<script>
document.write(window.location);
</script>


Where you want to output the URL. There are probably better ways to write this, depending on how you generate the surrounding code.

10% popularity Vote Up Vote Down


 

@Rambettina238

In PHP:

<php
//pull this using whatever method you like like a DB or URL-parser
//$url = 'http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]; //or some derivative
$url = "http://www.example.com/page.html"
?>

<div class="fb-like share-button fbbutton" data-href="<?php echo $url;?>" data-send="false" data-layout="button_count" data-width="450" data-show-faces="true"></div>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme