最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Undefined pathname in js - Stack Overflow

programmeradmin2浏览0评论

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.

Share edited Sep 20, 2011 at 1:10 David542 asked Sep 19, 2011 at 22:08 David542David542 111k206 gold badges571 silver badges1k bronze badges 1
  • 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 use window.location? – Alex Commented Sep 19, 2011 at 22:11
Add a ment  | 

2 Answers 2

Reset to default 5

https://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

发布评论

评论列表(0)

  1. 暂无评论