-1

I coded some basic share buttons in HTML and CSS. I want to place these buttons on 300 different pages on a simple HTML website.

Is there a easy way to ask for the current link of the website instead of typing in the website URL manually?

Example:

Page 1 www.site.com/home.html

<a class='share-facebook' href="http://www.facebook.com/sharer.php?u= www.site.com/home.html" target="_blank">

This will now share the home page through Facebook:

Page 2 www.site.com/info.html

<a class='share-facebook' href="http://www.facebook.com/sharer.php?u=[current website url-tag or code?]" target="_blank">

What do I type in at "current website URL tag or code" to get the desired outcome? (share www.info.html without actually manually typing in the URL)

Is it even possible with only HTML? If not, how should the JS look like?

halfer
  • 19,824
  • 17
  • 99
  • 186
Pbellum
  • 5
  • 1
  • 4

1 Answers1

0

No, this isn't possible with pure HTML. In fact, this is often done server-side using languages such as PHP:

<a class='share-facebook' href="http://www.facebook.com/sharer.php?u=<?="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"?>" target="_blank">

In JavaScript, you'll probably have to assign an id to your link first.

HTML:

<a class='share-facebook' href="http://www.facebook.com/sharer.php?u=[current website url-tag or code?]" target="_blank" id="facebook-button">

JavaScript:

document.getElementById("facebook-button").href="document.URL"; 
halfer
  • 19,824
  • 17
  • 99
  • 186
Huey
  • 5,110
  • 6
  • 32
  • 44