PDA

View Full Version : Remove parameter from URL without refreshing



gib65
02-26-2015, 04:57 PM
I need a way to remove parameters from the URL without refreshing the page. The only way I know of to change the URL is by saying:

window.location.href = ...

But this refreshes the page.

I also tried this:

window.history.replaceState({}, "", window.location.href.substring(0, position_of_parameter_to_remove));

but this also seems to refresh the page.

The reason I need to remove the parameter from the URL is as follows: I have two buttons on the page. One of them adds the parameter to the URL and reloads the page with it. When the page reloads, I use that parameter. The second button also reloads the page, but in this case, I want the parameter not to be there so that it doesn't get used when the page reloads. However, I have no control over this button (it's in a special "captivate" object that I have no access to). So I cannot tell it to reload the page with a URL that doesn't have the parameter. It just reloads the page "as is". So I'm trying to remove the parameter as soon as it gets retrieved and used. That way, the parameter will only exist in the URL when the first button is clicked and the page reloads.

But I still don't know how to do that without doing another page reload. Can anyone help me?

Beverleyh
02-26-2015, 05:12 PM
I can't make any firm suggestions without seeing your page, but in a case like this, I would try to concentrate on the factors that you are able to influence - in other words, can you rewrite the first button's function so that it doesn't set parameters in the URL?

For more specific help, please provide a link to your page or a simplified example in JSFiddle.

jscheuer1
02-26-2015, 05:47 PM
I would add:

I don't believe you can change (in real time - you can rewrite it so that the server will see and/or display it differently, and that might or might not satisfy your requirements - javascript cannot do this though, it must be done on the server side with .htaccess or some sort of configuration change) the url without reloading the page. They sort of define each other. The one exception is the hash (#) if that's what you mean, you can tell the browser to change that via javascript:


location.hash = '';

Modern browsers will not reload. This will change the hash from something like:

#top

to either nothing or to just a # with nothing after it, depending upon the browser.

Hashes can be complex and can look a lot like query strings and might be called parameters. If that's what you have, you can rewrite using javascript, otherwise you will have to edit the script that you seem to say you have no access to, or do something that may or may not work on the server side - assuming you have access to that part of your host's configuration.

Another option would be to do whatever it is you want to do in a different way that doesn't involve this problem.

If you don't know if you have a hash or not, we would need to see the page. If you don't have a hash, seeing the page might still reveal a workable approach.

gib65
03-02-2015, 11:02 PM
Thanks everyone for your help.