window.location
works 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?
Add a comment
|
4 Answers
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.
-
Thanks for your answer but is it supported in most browsers like firefox (i have checked it), chrome, ie (8,9,10) and safari? Jan 7, 2014 at 5:21
-
Try this:
location.protocol + "//" + location.host
-
-
Thanks for your answer but is it supported in most browsers like firefox (i have checked it), chrome, ie (8,9,10) and safari? Jan 7, 2014 at 5:23
I think it will ok for you
var base_url = window.location.origin;
var host = window.location.host;
var pathArray = window.location.pathname.split( '/' );