How to get request uri from location.href in javascript?

后端 未结 4 1796
余生分开走
余生分开走 2021-02-12 13:51

What I get from location.href is like this:

http://stackoverflow.com/questions/ask

But I only want to get questions/ask

相关标签:
4条回答
  • 2021-02-12 14:14

    The location object has a pathname property.

    This will give you /questions/ask and to remove the first character, use substring(1):

    var path = location.pathname.substring(1);
    
    0 讨论(0)
  • 2021-02-12 14:24

    location.pathname.substr(1) would that be.

    0 讨论(0)
  • 2021-02-12 14:35

    You can use location.pathname.substring(1)

    0 讨论(0)
  • 2021-02-12 14:38
    var uri = window.location.href.substr(window.location.protocol.length + window.location.hostname.length + 2);
    

    This code also includes GET and HASHTAGS (basically everything after hostname)

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