Javascript 如何将 URL 修剪/剥离到页面名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3213083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:41:48  来源:igfitidea点击:

How do I trim/strip the URL down to the page name?

javascriptjqueryhtml

提问by Barrie Reader

How would I go about trimming/stripping the URL down to the page name...

我将如何修剪/剥离 URL 到页面名称...

So: http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html

所以: http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html

Would become: apage.html

会成为: apage.html

Any ideas?

有任何想法吗?

回答by ccppjava

you do not need jquery:

你不需要jquery:

var url = window.location.href;
var page = url.substring(url.lastIndexOf('/') + 1);

Edit: a good point of the possible query string:

编辑:可能的查询字符串的一个好点:

// it might be from browser & / anywhere else
var url = window.location.href;
url = url.split('#').pop().split('?').pop();
var page = url.substring(url.lastIndexOf('/') + 1);

ok, if the location object is available, use pathname gives better result as show below, however, a url can be a string or something directly from text field or span/label. So above solution should have its place.

好的,如果位置对象可用,使用路径名会提供更好的结果,如下所示,但是,url 可以是字符串或直接来自文本字段或跨度/标签的内容。所以上面的解决方案应该有它的位置。

回答by bobince

With locationand any link (<a>) elements on the page, you get a load of properties that give you specific parts of the URL: protocol, host, port, pathname, searchand hash.

location任何链接(<a>)页面上的元素,你得到的是给你的URL的特定部分特性的负载:protocolhostportpathnamesearchhash

You should always use these properties to extract parts of the URL in preference to hacking about with hrefand probably getting it wrong for corner cases. For example, every solution posted here so far will fail if a ?queryor #fragmentis present. The answers from Rob and digitalFresh attempt to cope with them, but will still fail if a /character is present in the query string or fragment (which is valid).

您应该始终使用这些属性来提取 URL 的一部分,而不是在极端href情况下进行黑客攻击,并且可能会出错。例如,如果存在?query或 ,#fragment则到目前为止发布的每个解决方案都将失败。Rob 和 digitalFresh 的答案试图解决它们,但如果/查询字符串或片段(有效)中存在字符,则仍然会失败。

Instead, simply:

相反,只需:

var pagename= location.pathname.split('/').pop();

回答by epascarello

Most of the solutions here are not taking advantage of the window.location object. The location object has this wonderful thing called pathname which returns just the path, no query string, host, protocol, hash, etc.

这里的大多数解决方案都没有利用 window.location 对象。location 对象有一个叫做 pathname 的奇妙东西,它只返回路径,没有查询字符串、主机、协议、哈希等。

var mypage = window.location.pathname.split("/").pop();

回答by Rob

You could do something like this:

你可以这样做:

document.location.href.split('/').pop();

Edit: you probably want to get rid of the query string if there is one also:

编辑:如果还有查询字符串,您可能想删除它:

document.location.href.split('/').pop().split('?').shift();

Edit 2: this will also ignore an anchor in the url if there is one

编辑 2:如果有一个锚点,这也将忽略 url 中的锚点

document.location.href.split('/').pop().split(/\?|#/).shift();

回答by tcooc

This should also exclude query and hash values.

这也应该排除查询和哈希值。

var path = location.href;
path = path.substring(path.lastIndexOf("/") + 1);
path = path.split("?")[0].split("#")[0];
console.debug(path);

回答by Ben Everard

Haven't tested so compeltely guessed, but I'm sure something like this will do :-)

还没有测试过如此完全猜测,但我相信这样的事情会做:-)

var url = 'http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html';
var page = url.split('/');
alert(page[page.length-1]);?

EDITTested under jsfiddleand it was wrong, the above code should now work :-)

编辑jsfiddle测试,结果是错误的,上面的代码现在应该可以工作了 :-)