Get current URL with jQuery?

venkatachalam picture venkatachalam · Jan 2, 2009 · Viewed 2.2M times · Source

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

Answer

Ryan Doherty picture Ryan Doherty · Jan 2, 2009

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)