Updating existing URL querystring values with jQuery

前端 未结 9 828
逝去的感伤
逝去的感伤 2020-12-01 16:15

Let\'s say I have a url such as:

http://www.example.com/hello.png?w=100&h=100&bg=white

What I\'d like to do is update the values of

9条回答
  •  抹茶落季
    2020-12-01 16:50

    Simple solution

    You can use URLSearchParams.set() like below:

    var currentUrl = 'http://www.example.com/hello.png?w=100&h=100&bg=white';
    var url = new URL(currentUrl);
    url.searchParams.set("w", "200"); // setting your param
    var newUrl = url.href; 
    console.log(newUrl);
    

    Online demo (jsfiddle)

提交回复
热议问题