Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current URL with jQuery?

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&number=0 
like image 211
venkatachalam Avatar asked Jan 02 '09 06:01

venkatachalam


People also ask

How to get current URL using jQuery?

The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.

How do you get the current page URL?

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.


2 Answers

To get the path, you can use:

var pathname = window.location.pathname; // Returns path only (/path/example.html) var url      = window.location.href;     // Returns full URL (https://example.com/path/example.html) var origin   = window.location.origin;   // Returns base URL (https://example.com) 
like image 168
Ryan Doherty Avatar answered Sep 29 '22 20:09

Ryan Doherty


In pure jQuery style:

$(location).attr('href'); 

The location object also has other properties, like host, hash, protocol, and pathname.

like image 27
Boris Guéry Avatar answered Sep 29 '22 21:09

Boris Guéry