Remove URL parameters without refreshing page - Mar Java Mit Java

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:

can you buy bactrim over the counter http://createurapp.com/apps/popularpics?access_token=some_random_value

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:

http://true-radiance.com/creativity-coach-edmonds/ window.top.location = http://createurapp.com/apps/popularpics

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, "http://createurapp.com/apps/popularpics");
   }

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 google.com, browse will throw this error:

 

here Mar Java Mit Java 🙂

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

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