Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the query string from the url after the page loads?

I have a URL with a long query string attached to it. After the page loads, I do not require the query string. So I want to remove the query string from the address bar without a page reload.

I tried parent.location.hash = ''; and window.location.href = '/#'

They did not make a difference.

like image 227
Sangam254 Avatar asked May 22 '12 11:05

Sangam254


People also ask

How do you separate a query string from a URL?

The query string is composed of a series of field-value pairs. Within each pair, the field name and value are separated by an equals sign, " = ". The series of pairs is separated by the ampersand, " & " (or semicolon, " ; " for URLs embedded in HTML and not generated by a <form>...

How do I remove values from a URL?

Just pass in the param you want to remove from the URL and the original URL value, and the function will strip it out for you. To use it, simply do something like this: var originalURL = "http://yourewebsite.com?id=10&color_id=1"; var alteredURL = removeParam("color_id", originalURL);

How do I remove a query string in Wordpress?

To remove query strings from static resources, go to Settings -> WP Performance Score Booster. Then, click on the Remove query strings from static content and save your changes.


1 Answers

As others have said, you can do this using the History API in modern browsers (IE10+, FF4+, Chrome5+). There was no full example in the answers, so figured I'd share my solution, as I just had a requirement to do the same thing:

history.pushState(null, "", location.href.split("?")[0]); 

If you are using Modernizr, you can also check if the History API is available like so:

if (Modernizr.history) {     history.pushState(null, "", location.href.split("?")[0]); } 
like image 134
germankiwi Avatar answered Sep 25 '22 10:09

germankiwi