1

I am using javascript. How do I get the path of the current URL and assign it to my code? Here is my code :

$(document).ready(function() {
  $(".share").hideshare({
    link: "current_url",
    position: "top"
  });
});
2
  • Use window.location to get the current URL.
    – Himanshu
    Commented Apr 18, 2016 at 6:45
  • window.location.href will give you the current url
    – Manish
    Commented Apr 18, 2016 at 6:45

6 Answers 6

3

Try window.location.href

$(document).ready(function() {
  $(".share").hideshare({
    link: window.location.href,
    position: "top"
  });
});
0
3

Try with window.location.href or document.URL object

$(document).ready(function() {
  $(".share").hideshare({
    link: window.location.href,
    position: "top"
  });
});
0
3

Something like the following should do it:

$(document).ready(function() {
  $(".share").hideshare({
    link: location.href
    position: "top"
  });
});

The window.location (also can be referenced by location) property contains many utility functions related to the current page.

Such as window.location.hash for the anchor or window.location.search for the query string

0
3

You can use the window.location.href or window.location.path expressions if you don't need the full url.

1
  • window.location.path don't work on the latest version of Chrome/Firefox - window.location.pathname however should work
    – Brian
    Commented Apr 19, 2016 at 5:02
2

Use window.location.href or window.location.pathname expressions if you don't need the full url. I mean

$(document).ready(function() {
  $(".share").hideshare({
    link: "window.location.href/window.location.pathname",
    position: "top"
  });
});
3
  • window.location.href/window.location.pathname what is the difference? Commented Jun 29, 2016 at 10:20
  • Are you Blind ? Difference mentioned above the code.
    – user6492976
    Commented Jun 29, 2016 at 12:26
  • 1
    Sorry. Thank You for your reply. Commented Jun 30, 2016 at 6:13
1

To get Current URL, you can use:

var pathname = window.location.pathname; // Returns path only
var url      = window.location.href;     // Returns full URL

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.