简体   繁体   English

Jquery获取当前html文件的名称

[英]Jquery to get the name of the current html file

If you are on http://www.cnn.com/2012/11/09/opinion/brown-pakistan-malala/index.html can you get Jquery to grab the index.html ? 如果你在http://www.cnn.com/2012/11/09/opinion/brown-pakistan-malala/index.html ,你可以让Jquery抓住index.html吗?

or if you are on 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 have it return dd249cd0-216d-11e2-8448-81b1ce7d6978_story.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

And for non extension defined pages such as this current one http://stackoverflow.com/questions/13317276/jquery-to-get-the-name-of-the-current-html-file can it return the last "file" in the "directory"structure, for example: jquery-to-get-the-name-of-the-current-html-file 对于非扩展定义的页面,例如当前的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

Although not JQuery, you can access it using the following: 虽然不是JQuery,但您可以使用以下方法访问它:

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

or 要么

document.location.pathname.match(/[^\\/]+$/)[0] in case of unneeded anchors/hash tags (#). 在不需要的锚点/哈希标记(#)的情况下, document.location.pathname.match(/[^\\/]+$/)[0]

location.pathname.split('/').slice(-1)[0]

No need for jQuery. 不需要jQuery。 This will give you the last segment of the URL path (the bit after the last slash): 这将为您提供URL路径的最后一段(最后一个斜杠后面的位):

var href = document.location.href;
var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);
function getCurentFileName(){
    var pagePathName= window.location.pathname;
    return pagePathName.substring(pagePathName.lastIndexOf("/") + 1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM