In javascript, how can I get the relative path of the current url?
for example http://www.example.com/test/this?page=2
I want just the /test/this?page=2
In javascript, how can I get the relative path of the current url?
for example http://www.example.com/test/this?page=2
I want just the /test/this?page=2
I use this:
Reference: http://www.w3schools.com/jsref/prop_doc_url.asp
location.href.replace(location.origin,'');
Only weird case:
http://foo.com/ >> "/"
Try
holds the url of the page your script is running in.
You should use it the javascript way, to retrieve the complete path including the extensions from the page,
So, a path like this, can be retrieved too.
You can use the below snippet to get the absolute url of any page.
Checkout get absolute URL using Javascript for more details and multiple ways to achieve the same functionality.