I am trying to extract the base of the url:
-->
http://127.0.0.1:8000/something --> http://127.0.0.1:8000
When I try and use the following:
var pathArray = window.location.pathname;
alert(pathArray);
I get pathArray = undefined
. Note: using location.hostname
does work here. Why is .pathname
returning undefined, and how would I get the url base here? Thank you.
Update: I used var pathArray = 'http://' + window.location
.
I am trying to extract the base of the url:
http://google./something --> http://google.
http://127.0.0.1:8000/something --> http://127.0.0.1:8000
When I try and use the following:
var pathArray = window.location.pathname;
alert(pathArray);
I get pathArray = undefined
. Note: using location.hostname
does work here. Why is .pathname
returning undefined, and how would I get the url base here? Thank you.
Update: I used var pathArray = 'http://' + window.location
.
-
1
Path name does not return the domain name. It returns the pat after the domain name. If you're at
http://example./a/b.html
,window.location.pathname
returns/a/b.html
. Why not usewindow.location
? – Alex Commented Sep 19, 2011 at 22:11
2 Answers
Reset to default 5https://developer.mozilla/en-US/docs/Web/API/Window/location May not work in all browsers, as per usual
I'd just get window.location
and parse it.
Try window.location.host, it works for http://localhost:8000, so it should work for your case