11

In my website blog pages added query string in that page URL. I wanted to remove the query string from the URL. So i used to go with jquery and i wrote and added into my scripts. It removes the query string but keep on refreshing the page upto nth time. I used to "one" jquery method. That also doesn't work.

Can you help me

My Script is

jQuery(document).one('ready',function(){
    window.location.href = window.location.href.split('?')[0];
});
3
  • 1
    You need to check if window.location.href has ? first. Commented Mar 31, 2016 at 7:26
  • use .indexOf() to check. Commented Mar 31, 2016 at 7:27
  • The reason that you check that window.location.href has a ? is that the result of split is an array of at least one string. Try for yourself: "https://example.com".split('?')[0] results in https://example.com, thus causing a redirect loop when you keep updating window.location.href to itself. Commented May 6, 2024 at 15:43

4 Answers 4

29
var uri = window.location.href.toString();
if (uri.indexOf("?") > 0) {
    var clean_uri = uri.substring(0, uri.indexOf("?"));
    window.history.replaceState({}, document.title, clean_uri);
}
Sign up to request clarification or add additional context in comments.

2 Comments

It would be nice if you could put an explanatory text along with the code, to improve your answer, making it more helpful to all users
This should be marked as the correct answer!
8

setting a value to window.location.href will reload the page. Try this:

    var url = window.location.href;
    var a = url.indexOf("?");
    var b =  url.substring(a);
    var c = url.replace(b,"");
    url = c;

1 Comment

It is just removing the query params from var URL, not actually updating the browser url
7

keep on refreshing the page upto nth time.

This is because you are not checking whether the URL has a query string or not. So it is an infinite refresh.

You can try this:

$(document).ready(function(){
    if (window.location.href.indexOf('?') > -1) {
        window.location.href = window.location.pathname;
    }
});

Edit 1: Is it possible to remove query string without page refresh?

You can try this:

$(document).ready(function(){
    if (window.location.href.indexOf('?') > -1) {
        history.pushState('', document.title, window.location.pathname);
    }
});

3 Comments

Thanks. Yes its working. However it get refreshing one time. Is it possible to remove query string without page refresh?
@Beginner hey this second script is cool!But i still see for about second the query :( also does the query still holds values?
@IngusGraholskis You will, until the DOM has been loaded. No, it won't be available client side.
0

So if the href is different to the hostname + pathname it has an query. So remove it and refresh the page once:

jQuery(document).one('ready',function(){
    if(window.location.href != window.location.hostname + window.location.pathname) {
        window.location.href = window.location.pathname;
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.