js中未定义的路径名

时间:2011-09-19 22:08:04

标签: javascript

我正在尝试提取网址的基础:

http://google.com/something --> http://google.com
http://127.0.0.1:8000/something --> http://127.0.0.1:8000

当我尝试使用以下内容时:

var pathArray = window.location.pathname;
alert(pathArray);

我得到pathArray = undefined。注意:使用location.hostname可以在这里工作。为什么.pathname返回undefined,我如何获取url base?谢谢。

更新:我使用了var pathArray = 'http://' + window.location

2 个答案:

答案 0 :(得分:5)

https://developer.mozilla.org/en/window.location 可能不适用于所有浏览器,按照惯例

我只是得到window.location并解析它。

答案 1 :(得分:-1)

尝试使用window.location.host,它适用于http://localhost:8000,因此它适用于您的情况

相关问题