js中未定义的路径名



我正在尝试提取url的基础:

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返回未定义,我将如何得到url基础在这里?谢谢你。

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

https://developer.mozilla.org/en-US/docs/Web/API/Window/location可能无法在所有浏览器中工作,如往常一样

我只需获取window.location并解析它

试试window.location。主机,它适用于http://localhost:8000,所以它应该适用于您的情况

相关内容

最新更新