Get relative path of the page url using javascript

后端 未结 7 1158
温柔的废话
温柔的废话 2020-12-29 19:48

In javascript, how can I get the relative path of the current url?

for example http://www.example.com/test/this?page=2

I want just the /te

7条回答
  •  失恋的感觉
    2020-12-29 20:30

    You can use the below snippet to get the absolute url of any page.

     var getAbsoluteUrl = (function() {
         var a;
         return function(url) {
             if(!a) a = document.createElement('a');
             a.href = url;
             return a.href;
         }
    })();
    
    // Sample Result based on the input.
    getAbsoluteUrl('/'); //Returns http://stackoverflow.com/
    

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

提交回复
热议问题