获取url路径的第一部分

如果我有如下url:

http://localhost:53830/Organisations/1216/View

我想以小写格式提醒url路径的第一部分,例如“Organizations”

到目前为止,我已经:

var first=$(位置).attr('pathname');
第一,指数f(1);
首先,替换(“/”,“”);
首先,toLowerCase();
警报(第一);

但它并没有按预期工作。有人能帮忙吗?谢谢

这永远不会引发错误,因为路径名始终以/开头,因此拆分后生成的数组的最小长度将为2:

const firstPath=window.location.pathname.split('/')[1];

如果我们在域根目录下,返回的值将是空字符串

发表评论