29

如果你在,http://www.cnn.com/2012/11/09/opinion/brown-pakistan-malala/index.html你能让 Jquery 抓住index.html吗?

或者如果你在http://www.washingtonpost.com/politics/decision2012/supreme-court-to-review-key-section-of-voting-rights-act/2012/11/09/dd249cd0-216d-11e2-8448-81b1ce7d6978_story.html让它回来dd249cd0-216d-11e2-8448-81b1ce7d6978_story.html

对于非扩展定义的页面,例如当前页面,http://stackoverflow.com/questions/13317276/jquery-to-get-the-name-of-the-current-html-file它可以返回“目录”结构中的最后一个“文件”,例如:jquery-to-get-the-name-of-the-current-html-file

4

4 回答 4

48

虽然不是 JQuery,但您可以使用以下方式访问它:

document.location.href.match(/[^\/]+$/)[0]

或者

document.location.pathname.match(/[^\/]+$/)[0]在不需要的锚点/哈希标签 (#) 的情况下。

于 2012-11-09T23:00:36.850 回答
24
location.pathname.split('/').slice(-1)[0]
于 2012-11-09T23:14:08.273 回答
14

不需要 jQuery。这将为您提供 URL 路径的最后一段(最后一个斜杠之后的位):

var href = document.location.href;
var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);
于 2015-06-06T04:16:48.530 回答
6
function getCurentFileName(){
    var pagePathName= window.location.pathname;
    return pagePathName.substring(pagePathName.lastIndexOf("/") + 1);
}
于 2012-11-09T23:02:10.710 回答