Javascript – Adding a parameter to the URL with JavaScript


In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:

Original URL:


Resulting URL:


Looking for a JavaScript function which parses the URL looking at each parameter, then adds the new parameter or updates the value if one already exists.

Best Solution

You can use one of these:


var url = new URL("");

// If your expected result is ""
url.searchParams.append('x', 42);

// If your expected result is ""
url.searchParams.set('x', 42);