Remove URL parameters without refreshing page

I recently had a requirement where I wanted to remove the unwanted query strings from the URL address bar without refreshing, redirecting or reloading the page.

The URL was like this:

I wanted to remove the query string as many users had the tendency of sharing the whole URL when it meant sense to share the URL only without the query parameter.

One option was to reload the page by redirecting it to the URL without query string like this: =

But I did not want the unnecessary reload. One of the reasons being an unwanted increase in google Adsense ads impressions.

Fortunately, there is a simple way of changing the URL from URL address bar. Here is the javascript code to do so :

if(window.history != undefined && window.history.pushState != undefined) {
        // if pushstate exists, add a new state the the history, this changes the url without reloading the page
        window.history.pushState({}, document.title, "");

So this basically pushes the new URL(without query parameter in this case) into the window history.

We can not change the domain though. Base domain needs to remain same. For example, if I try to change the above URL to say, browse will throw this error:


Mar Java Mit Java 🙂

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *