append to url and refresh page

后端 未结 11 2218
终归单人心
终归单人心 2020-12-07 14:07

I am looking to write a piece of javascript that will append a parameter to the current url and then refresh the page - how can I do this?

11条回答
  •  爱一瞬间的悲伤
    2020-12-07 14:43

    If you are developing for a modern browser, Instead of parsing the url parameters yourself- you can use the built in URL functions to do it for you like this:

    const parser = new URL(url || window.location);
    parser.searchParams.set(key, value);
    window.location = parser.href;
    

提交回复
热议问题