Updating existing URL querystring values with jQuery

前端 未结 9 829
逝去的感伤
逝去的感伤 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:55

    I like Ali's answer the best. I cleaned up the code into a function and thought I would share it to make someone else's life easier. Hope this helps someone.

    function addParam(currentUrl,key,val) {
        var url = new URL(currentUrl);
        url.searchParams.set(key, val);
        return url.href; 
    }
    

提交回复
热议问题