How to get the base path in jQuery?

前端 未结 4 1845
囚心锁ツ
囚心锁ツ 2021-02-03 19:26

window.locationworks fine, but returns me the whole, absolute path, like http://domain.xyz/punch/lines. But I only need http://domain.xyz/

相关标签:
4条回答
  • 2021-02-03 19:59

    I think it will ok for you

    var base_url = window.location.origin;
    
    var host = window.location.host;
    
    var pathArray = window.location.pathname.split( '/' );
    
    0 讨论(0)
  • 2021-02-03 20:06

    Try this:

    location.protocol + "//" + location.host
    
    0 讨论(0)
  • 2021-02-03 20:11

    You can get the protocol and the host separately, and then join them to get what you need

    window.location.protocol + "//" + window.location.host + "/"
    

    As a sidenote, window.location.pathname would contain the path.

    0 讨论(0)
  • 2021-02-03 20:13

    You can use this statement

    var baseUrl = document.location.origin;
    
    0 讨论(0)
提交回复
热议问题