how to replace part of the URL with JavaScript?

The only way to update the displayed URL without reloading the page is history.pushState

window.history.pushState('', '', '/your-new-url');

My version. Works fine for me.

let url = window.location.toString();
window.history.pushState('', '', url.replace(searching_string, new_string));

 location.href = location.href.replace(
    'function=search&', 'function=loginsearch&user=admin&password=admin&')

var url = window.location.toString();
window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');

Tags:

Javascript