Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript to reload the page as GET request

I have a seemingly simple question, but can't find the answer. I have a webpage, which may have resulted from a POST request and may have an anchor (#) in the URL. I want to reload this page as a GET request in JavaScript. So it's similar to this question, but I actually want to avoid the POST, not just the warning about it.

So, for example, if the page resulted from a POST request to "http://server/do/some?thing#" I want to reload the URL "http://server/do/some?thing" as a GET. If I try

window.location.reload(true);

that causes IE to try a POST. If I instead do:

window.location = window.location.href;

this does nothing when the URL has an anchor. Do I really need to do string manipulation myself to get rid of the "#whatever" or is there an easier, "better" way to do this?

like image 815
EMP Avatar asked Aug 04 '09 01:08

EMP


People also ask

How do I reload a page in JavaScript?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

How do you reload an object in JavaScript?

It can be done as given below: <button onclick = "location. reload()"> Reload </button>

What is location reload () in JavaScript?

The location. reload() method reloads the current URL, like the Refresh button.

How do I force a page to reload in HTML?

Window location.reload() The reload() method reloads the current document. The reload() method does the same as the reload button in your browser.


1 Answers

The best I've come up with so far is:

function reloadAsGet()
{
    var loc = window.location;
    window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
}
like image 102
EMP Avatar answered Oct 19 '22 23:10

EMP