I am using jQuery. How do I get the path of the current URL and assign it to a variable?
Example URL:
http://localhost/menuname.de?foo=bar&nu
If you need the hash parameters present in the URL, window.location.href
may be a better choice.
window.location.pathname
=> /search
window.location.href
=> www.website.com/search#race_type=1
I have this to strip out the GET variables.
var loc = window.location;
var currentURL = loc.protocol + '//' + loc.host + loc.pathname;
See purl.js. This will really help and can also be used, depending on jQuery. Use it like this:
$.url().param("yourparam");
You'll want to use JavaScript's built-in window.location object.
Just add this function in JavaScript, and it will return the absolute path of the current path.
function getAbsolutePath() {
var loc = window.location;
var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}
I hope it works for you.
You can simply get your path using js itself, window.location
or location
will give you the object of current URL
console.log("Origin - ",location.origin);
console.log("Entire URL - ",location.href);
console.log("Path Beyond URL - ",location.pathname);