How to get the directory part of current URL in JavaScript?

前端 未结 6 676
生来不讨喜
生来不讨喜 2021-01-11 12:42

I\'m trying to add a \"back to dir\" button at the top of a web page, which would redirect the user to the same URL, but with no filename in it.

For example, clickin

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 13:20

    /* to avoid query parameters, use pathname instead of href */
    var l = document.location.pathname.split('/');
    l.pop();
    console.log(document.location.origin + l.join('/'));
    

提交回复
热议问题