Get relative URL from absolute URL

后端 未结 8 1924
自闭症患者
自闭症患者 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条回答
  •  庸人自扰
    2020-12-03 05:17

    Below you can find script on that here we have only one problem which is one manual work, we want to add split attribute ie, if your site link is: Get relative URL from absolute URL

    we want to url as below: /questions/6263454/get-relative-url-from-absolute-url

    so in below code we have to add .com instead of .net ie, var res = at.split(".net"); here we want to add as per our requirement now i need to seperate after .com so code will be "var res = at.split(".com");".

    i think you guys got that point if you have still doubt let me know please and to see changes in code by inspecting:

    $(document).ready(function(){
    	$("a").each(function(){
        var at= $(this).attr("href");
        var res = at.split(".net");
        var res1 = res[0];
        var res2 = res[1];
        alert(res2);
        $(this).attr("href",res2);
      });
    });
    
    
    

提交回复
热议问题