How to replace url parameter with javascript/jquery?

前端 未结 17 2394
旧时难觅i
旧时难觅i 2020-11-28 04:06

I\'ve been looking for an efficient way to do this but haven\'t been able to find it, basically what I need is that given this url for example:

http://localh         


        
17条回答
  •  情书的邮戳
    2020-11-28 04:39

    In modern browsers (everything except IE9 and below), our lives are a little easier now with the new URL api

    var url = new window.URL(document.location); // fx. http://host.com/endpoint?abc=123
    url.searchParams.set("foo", "bar");
    console.log(url.toString()); // http://host/endpoint?abc=123&foo=bar
    url.searchParams.set("foo", "ooft");
    console.log(url.toString()); // http://host/endpoint?abc=123&foo=ooft
    

提交回复
热议问题