[javascript] How do I reload a page without a POSTDATA warning in Javascript?

I want to reload a page using:

window.location.reload(true); 

But I receive the POSTDATA warning because the refresh function want to resend previous POST form data. How can I refresh my page without this warning?

UPDATED: I have no control of the project! I can't workaround the POST itself!

This question is related to javascript refresh reload postdata

The answer is


I had some problems with anchor/hash-urls (including #) not reloading using the solution from Rex...

So I finally ended up by removing the hash part:

window.location = window.location.href.split("#")[0];

Nikl's version doesn't pass get query parameters, I used the following modified version:

window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname + window.location.search;

or in my case I needed to refresh the topmost page\frame, so I used the following version

window.top.location.href = window.top.location.protocol +'//'+ window.top.location.host + window.top.location.pathname + window.top.location.search;

To bypass POST warning you must reload page with full URL. Works fine.

window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname;

You can't refresh without the warning; refresh instructs the browser to repeat the last action. It is up to the browser to choose whether to warn the user if repeating the last action involves resubmitting data.

You could re-navigate to the same page with a fresh session by doing:

window.location = window.location.href;

I've written a function that will reload the page without post submission and it will work with hashes, too.

I do this by adding / modifying a GET parameter in the URL called reload by updating its value with the current timestamp in ms.

var reload = function () {
    var regex = new RegExp("([?;&])reload[^&;]*[;&]?");
    var query = window.location.href.split('#')[0].replace(regex, "$1").replace(/&$/, '');
    window.location.href =
        (window.location.href.indexOf('?') < 0 ? "?" : query + (query.slice(-1) != "?" ? "&" : ""))
        + "reload=" + new Date().getTime() + window.location.hash;
};

Keep in mind, if you want to trigger this function in a href attribute, implement it this way: href="javascript:reload();void 0;" to make it work, successfully.

The downside of my solution is it will change the URL, so this "reload" is not a real reload, instead it's a load with a different query. Still, it could fit your needs like it does for me.


Here's a solution that should always work and doesn't remove the hash.

let currentPage = new URL(window.location.href);
currentPage.searchParams.set('r', (+new Date * Math.random()).toString(36).substring(0, 5));
window.location.href = currentPage.href;

You can use JavaScript:

window.location.assign(document.URL);

Worked for me on Firefox and Chrome

check this link: http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get


If you are at the stage where you are finished with the post data and simply want to view the page again afresh, you could just use a window.location and even maybe append a random string as a query paramater to guarantee a new version of the page.


This worked

<button onclick="window.location.href=window.location.href; return false;">Continue</button>

The reason it didn't work without the

return false;
is that previously it treated that as a form submit button. With an explicit return false on it, it doesn't do the form submit and just does the reload of the same page that was a result of a previous POST to that page.


If you use GET method instead of POST then we can't the form filed values. If you use window.opener.location.href = window.opener.location.href; then we can fire the db and we can get the value but only thing is the JSP is not refreshing eventhough the scriplet having the form values.


using meta refresh in html

<meta http-equiv='refresh' content='1'>

using meta refresh in php

echo "<meta http-equiv='refresh' content='1'>"

The other solutions with window.location didn't work for me since they didn't make it refresh at all, so what I did was that I used an empty form to pass new and empty postdata to the same page. This is a way to do that based on this answer:

function refreshAndClearPost() {
    var form = document.createElement("form");
    form.method = "POST";
    form.action = location.href;
    form.style.display = "none";

    document.body.appendChild(form);
    form.submit();    //since the form is empty, it will pass empty postdata
    document.body.removeChild(form);
}

<html:form name="Form" type="abc" action="abc.do" method="get" onsubmit="return false;">

method="get" - resolves the problem.

if method="post" then only warning comes.


you are not forced to use javascript to do every thing. Many problems have easy solutions like this one. you can use this tricky anchor:

<a href=".">Continue</a>

of course I know you may need an automatic solution but this will help in many cases.

good luck


how about window.location.replace(window.location.href);


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to refresh

How to Refresh a Component in Angular Angular + Material - How to refresh a data source (mat-table) How to Update a Component without refreshing full page - Angular How to reload page the page with pagination in Angular 2? Swift: Reload a View Controller Button that refreshes the page on click Update data on a page without refreshing How to refresh or show immediately in datagridview after inserting? Gradle project refresh failed after Android Studio update Refresh Part of Page (div)

Examples related to reload

How to reload page the page with pagination in Angular 2? Button that refreshes the page on click How to reload or re-render the entire page using AngularJS PHP refresh window? equivalent to F5 page reload? How do I detect a page refresh using jquery? Reload the page after ajax success How to reload a div without reloading the entire page? One time page refresh after first page load How can I refresh a page with jQuery? How to reload a page using JavaScript

Examples related to postdata

How do I reload a page without a POSTDATA warning in Javascript?