获取当前html文件的名称

时间:2021-05-08 23:45:23

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/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

And for non extension defined pages such as this current one http://*.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://*.com/questions/13317276/jquerto -get- of- current-html-file,它能返回“目录”结构中的最后一个“文件”吗,例如:jquerto -get-the- of- current-html-file

4 个解决方案

#1


40  

Although not JQuery, you can access it using the following:

虽然不是JQuery,但您可以通过以下方式访问:

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

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

or

document.location.pathname.match(/[^\/]+$/)[0] in case of unneeded anchors/hash tags (#).

document.location.pathname.match(/[/ ^ \]+ /美元)[0],以防不必要的锚/散列标签(#)。

#2


17  

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

#3


11  

No need for jQuery. This will give you the last segment of the URL path (the bit after the last slash):

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

var href = document.location.href;
var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);

#4


3  

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

#1


40  

Although not JQuery, you can access it using the following:

虽然不是JQuery,但您可以通过以下方式访问:

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

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

or

document.location.pathname.match(/[^\/]+$/)[0] in case of unneeded anchors/hash tags (#).

document.location.pathname.match(/[/ ^ \]+ /美元)[0],以防不必要的锚/散列标签(#)。

#2


17  

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

#3


11  

No need for jQuery. This will give you the last segment of the URL path (the bit after the last slash):

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

var href = document.location.href;
var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);

#4


3  

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