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. Commented Apr 18, 2016 at 6:45
  • window.location.href will give you the current url 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"
  });
});
Sign up to request clarification or add additional context in comments.

Comments

3

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

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

Comments

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

Comments

3

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

1 Comment

window.location.path don't work on the latest version of Chrome/Firefox - window.location.pathname however should work
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 Comments

window.location.href/window.location.pathname what is the difference?
Are you Blind ? Difference mentioned above the code.
Sorry. Thank You for your reply.
1

To get Current URL, you can use:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.