How to get the base path in jQuery?

前端 未结 4 2004
囚心锁ツ
囚心锁ツ 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 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.

提交回复
热议问题