How to get base path in jQuery?

window.location works fine, but returns me the full, absolute path, e.g. http://domain.xyz/punch/lines . But I only need http://domain.xyz/ . How can I extract only this first part? And how can I make this dynamic, I want to be always the same, even when the path of the subdirectory increases?

+32
jquery url href path location
Aug 01 '13 at 11:17
source share
3 answers

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

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

As a support, window.location.pathname will contain the path.

+64
Aug 01 '13 at 11:20
source share

You can use this operator

 var baseUrl = document.location.origin; 
+18
Mar 01 '15 at 22:52
source share

Try the following:

 location.protocol + "//" + location.host 
+1
Aug 01 '13 at 11:20
source share



All Articles