David542
David542

Reputation: 110163

Undefined pathname in js

I am trying to extract the base of the url:

http://google.com/something --> http://google.com
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.

Upvotes: 3

Views: 6229

Answers (2)

Halcyon
Halcyon

Reputation: 57719

https://developer.mozilla.org/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.

Upvotes: 5

Balaarjunan
Balaarjunan

Reputation: 9

Try window.location.host, it works for http://localhost:8000, so it should work for your case

Upvotes: -1

Related Questions