开发者

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 pat开发者_如何转开发hArray = 'http://' + window.location.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜