How to get the base path in jQuery?

leymannx picture leymannx · Aug 1, 2013 · Viewed 97.8k times · Source

window.locationworks fine, but returns me the whole, absolute path, like http://domain.xyz/punch/lines. But I only need http://domain.xyz/. How can I extract only that first part? And how can I make that dynamic, I mean to be always the same even when the subdirectory path gets longer?

Answer

adeneo picture adeneo · Aug 1, 2013

You can get the protocol and the host separately, and then join them to get what you need

window.location.protocol + "//" + window.location.host + "/"

As a sidenote, window.location.pathname would contain the path.