Php

Javascript – change url variable

In modern browsers (everything except IE9 and below), our lives are a little easier now with the new URL api

var url = new window.URL(document.location); // fx. http://host.com/endpoint?abc=123
url.searchParams.set("foo", "bar");
console.log(url.toString()); // http://host/endpoint?abc=123&foo=bar
url.searchParams.set("foo", "ooft");
console.log(url.toString()); // http://host/endpoint?abc=123&foo=ooft

Source: https://stackoverflow.com/a/61869786/3476347

5 thoughts on “Javascript – change url variable
  1. I love looking through a post that can make people think. Also, many thanks for permitting me to comment!

Leave a Reply

Your email address will not be published.