How to replace url parameter with javascript/jquery?

前端 未结 17 2324
旧时难觅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:27

    Nowdays that's possible with native JS

    var href = new URL('https://google.com?q=cats');
    href.searchParams.set('q', 'dogs');
    console.log(href.toString()); // https://google.com/?q=dogs
    

提交回复
热议问题