Change URL parameters

前端 未结 26 2189
孤独总比滥情好
孤独总比滥情好 2020-11-22 08:40

I have this URL:

site.fwx?position=1&archiveid=5000&columns=5&rows=20&sorting=ModifiedTimeAsc

what I need is to be able to

26条回答
  •  孤街浪徒
    2020-11-22 09:13

    URL query parameters can be easily modified using URLSearchParams and History interfaces:

    // Construct URLSearchParams object instance from current URL querystring.
    var queryParams = new URLSearchParams(window.location.search);
     
    // Set new or modify existing parameter value. 
    //queryParams.set("myParam", "myValue");
    queryParams.set("rows", "10");
     
    // Replace current querystring with the new one.
    history.replaceState(null, null, "?"+queryParams.toString());
    

    Alternatively instead of modifying current history entry using replaceState() we can use pushState() method to create a new one:

    history.pushState(null, null, "?"+queryParams.toString());
    

    https://zgadzaj.com/development/javascript/how-to-change-url-query-parameter-with-javascript-only

提交回复
热议问题