Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get relative path of the page url using javascript

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

like image 739
raklos Avatar asked Dec 21 '10 22:12

raklos


People also ask

How do I get the relative URL in HTML?

To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.

How do I get the URL of a JavaScript page?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.

What is path relative URL?

A relative URL is a URL that only includes the path. The path is everything that comes after the domain, including the directory and slug. Because relative URLs don't include the entire URL structure, it is assumed that when linking a relative URL, it uses the same protocol, subdomain and domain as the page it's on.


2 Answers

Try

window.location.pathname+window.location.search 
like image 100
Dr.Molle Avatar answered Oct 18 '22 03:10

Dr.Molle


location.href 

holds the url of the page your script is running in.

like image 28
Bernhard Hofmann Avatar answered Oct 18 '22 03:10

Bernhard Hofmann