-3

I am using html javascript to share a link on facebook. I am using this code:

<a href="javascript:"onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=test.com');"     target="_blank">

Share on Facebook

Now i want to replace test.com with the current url link in the browser . How to do it !

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • possible duplicate of [Get current URL in JavaScript?](http://stackoverflow.com/questions/406192/get-current-url-in-javascript) // Your question is rather general, and not directly related to what the `facebook` tag stands for (tag removed), and could have resolved by doing a little proper research on your own. – CBroe Jun 06 '15 at 16:36

3 Answers3

1

You can use window.location or window.location.href which provides the current URL. In your example, you can use something like the following:

<a href="javascript:" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location);" target="_blank">

For more information see Window.location on Mozilla.org which has the following description:

The Window.location read-only property returns a Location object with information about the current location of the document.

Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: location = 'http://www.example.com' is a synonym of location.href = 'http://www.example.com'.

Grokify
  • 15,092
  • 6
  • 60
  • 81
0

I believe it should work if you use window.location.href, so your code would be the following:

<a href="javascript:"onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location.href);"     target="_blank">
Austin
  • 84
  • 3
0

window.location.href will get the current url e.g.

window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location.href);
Stephen S
  • 453
  • 2
  • 9