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

前端 未结 6 708
生来不讨喜
生来不讨喜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-11 13:25

    console.log(new URL(document.URL));

    you would get all object like this :

    {
      href:"https://yy.xx.id/index.php/5351/user/private/images%20%282%29.jpeg?forcedownload=1",
    
      origin:"https://smi.kerjaindonesia.id", 
      protocol: "https:", 
      username: "", 
      password: "", 
      pathname: "index.php/5351/user/private/images%20%282%29.jpeg"  ,
      port: "" ,
      protocol: "https:",
      search: "?forcedownload=1"
    }
    

提交回复
热议问题