jquery:修剪掉网址的最后一部分
我有这些网址,
http://mysite.net/home
http://mysite.net/projects
http://mysite.net/links
我想修剪主页、项目、链接等,所以我只有这个 http://mysite.net /
但如果我在 URL http://mysite.net/
谢谢。
I have these URLs,
http://mysite.net/home
http://mysite.net/projects
http://mysite.net/links
And I want to trim off home, projects, links ect, so I only have this http://mysite.net/
But it should not trim anything off if I have this in the URL http://mysite.net/
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用普通的 JavaScript 来说:
应该这样做。
编辑:哎呀,错过了斜杠:-)
In plain JavaScript:
should do it.
Edit: Whoops, missed slashes :-)
只需在javascript中获取location.origin,与jquery无关
just pick up location.origin in javascript, nothing to do with jquery
这是一种方法:
我在这里假设您不仅仅获得当前网站的域名,您所追求的是基于字符串(可能是另一个网站)的结果。
Here's one way to do it:
I'm assuming here that you're not just getting the domain of the current website and what you're after is a result based on a string (possible of another website).
如果 url 是纯字符串而不是“windows.location”,则可以使用简单的正则表达式来执行此操作
If url is a pure string instead of 'windows.location', you could use a simple regex to do this
您可以只使用
substring
和indexOf
:示例: http: //jsfiddle.net/niklasvh/HcUhj/
You could just use
substring
andindexOf
:example: http://jsfiddle.net/niklasvh/HcUhj/