views:

272

answers:

4

I'm triggering a page reload through javascript, using the following:

window.location.reload(true);

However, in some cases (of previous postback), the browser is giving me the following warning "To Display the webpage again, the web browser needs to resend the information you've previously submitted...".

Is there any way of avoing this message and just doing the postback anyway, because this might be confusing for the users? If I need to reload the page by another means, so be it.

A: 

The only way would be to stop it from rePOSTing the params. Use window.location = window.location

balupton
This will, however, lose `POST` data when converting to `GET` (I believe).
jensgram
+1  A: 

The request you're trying to do again is a POST request, hence the warning. You can either make sure that you're only reloading resources fetched via GET requests or - alternatively - submit a hidden <form> via the POST method (and thus simulating a reload guaranteed not to be cached).

As you probably already know the warning has to do with idempotence (the lack of side effects) within the HTTP verbs. GET (amongst others) are (should be, at least) considered idempotent, whereas POST requests allow for changes on the server. Therefore, the client should prompt the user to verify that s/he intends to perform the action again.

jensgram
+1  A: 

I don't think there is any way to do a postback without the message when you are doing the reload.

Btw, whats the reload for? If it is for submitting the POST information again, i suggest you manually submit the data through ajax or a hidden form submission using javascript.

mays
The reload is for showing updated data, and also for preserving state data (for example when the user has set up a filter on the page, I want to keep that filter active on reload)
Sam
So if you don't want to send the POST data again to the server,(which is what i understood from ur reply above) do a 303/302 redirect to the view page after the POST data is processed in the server side script. If you do that, you can do the same javascript reload stuff you were doing without any issues. See this for an explanation of redirect-after-POST solution. http://www.ajaxray.com/blog/2008/01/12/how-to-avoid-postdata-resend-warning/
mays
Thanks for the comment, I'm guessing the only way to do a 302 conditionally would be server-side then, or is there any way of doing it through javascript?
Sam
Logically its on the server side + its better to issue a 303 for this than 302.
mays
A: 

One way of preserving state data within a page is with fragment identifiers on the url, eg. #filter1, #filter2, and then using the handy hashchange plugin for jQuery to execute code (i.e., reload the relevant part of the page with fresh results) when the url fragment changes via the onhashchange event. It's also nice because the different states can be bookmarked and cached.

http://benalman.com/projects/jquery-hashchange-plugin/

weston