Jquery to get the name of the current html file

后端 未结 4 1970
温柔的废话
温柔的废话 2020-12-15 16:16

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?

or if you a

相关标签:
4条回答
  • 2020-12-15 16:49
    function getCurentFileName(){
        var pagePathName= window.location.pathname;
        return pagePathName.substring(pagePathName.lastIndexOf("/") + 1);
    }
    
    0 讨论(0)
  • 2020-12-15 16:50

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

    var href = document.location.href;
    var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);
    
    0 讨论(0)
  • 2020-12-15 16:58
    location.pathname.split('/').slice(-1)[0]
    
    0 讨论(0)
  • 2020-12-15 17:15

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

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

    or

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

    0 讨论(0)
提交回复
热议问题