Get relative URL from absolute URL

后端 未结 8 1952
自闭症患者
自闭症患者 2020-12-03 04:42

I want to get the relative URL from an absolute URL in JavaScript using regex and the replace method.

I tried the following but it is not working:

va         


        
8条回答
  •  猫巷女王i
    2020-12-03 05:09

    Below snippet returns the absolute URL of the page.

     var myURL = window.location.protocol + "//" + window.location.host  + window.location.pathname;
    

    If you need only the relative url just use below snippet

     var myURL=window.location.pathname;
    

    Checkout get relative URL using Javascript for more details and multiple ways to achieve the same functionality.

提交回复
热议问题