获取 url 路径的第一部分

发布于 2024-12-14 22:00:17 字数 389 浏览 0 评论 0原文

如果我有一个像这样的网址:

http://localhost:53830/Organizations/1216/View

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

到目前为止,我已经:

var first = $(location).attr('pathname');

first.indexOf(1);

first.replace('/', '');

first.toLowerCase();

alert(first);

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

If I have a url like:

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

I want to alert the first part of the url path in lowercase format e.g. 'organisations'

So far I have:

var first = $(location).attr('pathname');

first.indexOf(1);

first.replace('/', '');

first.toLowerCase();

alert(first);

but it's not working as intended. Can anyone help? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怎樣才叫好 2024-12-21 22:00:17

这永远不会抛出错误,因为路径名总是以 / 开头,因此拆分后结果数组的最小长度将为 2:

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

如果我们位于域根,则返回值将为空字符串''


const path = location.pathname.split('/');

path = [
  '', 
  'questions', 
  '8082239', 
  'get-the-first-part-of-a-url-path', 
  '8082346'
];

This will never throw an error because pathname always starts with a /, so the minimum length of the resulting array will be two after splitting:

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

If we are at the domain root, the returned value will be an empty string ''


const path = location.pathname.split('/');

path = [
  '', 
  'questions', 
  '8082239', 
  'get-the-first-part-of-a-url-path', 
  '8082346'
];
乖乖公主 2024-12-21 22:00:17
var first = $(location).attr('pathname');

first.indexOf(1);

first.toLowerCase();

first = first.split("/")[1];

alert(first);
var first = $(location).attr('pathname');

first.indexOf(1);

first.toLowerCase();

first = first.split("/")[1];

alert(first);
葬心 2024-12-21 22:00:17

您可以使用 URL

const first = new URL(location.href).pathname.split("/")[1]

You can use URL

const first = new URL(location.href).pathname.split("/")[1]
阳光①夏 2024-12-21 22:00:17

尝试使用 first.split('/') 这样你最终会得到一个字符串数组

['http:' ,'',  'localhost:53830' ,  'Organisations' ,  '1216' ,  'View' ]  

,然后找到 localhost:53830 之后的字符串

try to use first.split('/') so you will end up with an array of strings like

['http:' ,'',  'localhost:53830' ,  'Organisations' ,  '1216' ,  'View' ]  

then find the one the is right after localhost:53830

乖乖公主 2024-12-21 22:00:17

您可能需要添加 window 关键字,这样就不会出现 error:Cannot read property 'pathname' of undefined

var location = window.location.pathname.split('/')[1]

You may need to add window keyword so you don't get error:Cannot read property 'pathname' of undefined

var location = window.location.pathname.split('/')[1]
世界和平 2024-12-21 22:00:17

JS

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

jQuery

var first = $(location).attr('pathname');  first.indexOf(1);  first.toLowerCase();  first = first.split("/")[1];  alert(first); 

JS

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

jQuery

var first = $(location).attr('pathname');  first.indexOf(1);  first.toLowerCase();  first = first.split("/")[1];  alert(first); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文