With the following htlm:
<html>
<body>
<form action="#">
<p><input type="text"></input></p>
</form>
</body>
</html>
On load the input has an empty value. Change it to "1" and enter (submit) The page will show a "1" Pressing back will prefill the page with the entered "1" instead of the initial state (empty)
My real world application is an asp.net page where the top of the page contains search inputs and the bottom the results. When pressing back the fields are prefilled with values that don't correspond with the values shown in the results shown below.
How can I make it so that the browser's back button will prefill the page with the values that correspond with the initial state of the history's corresponding page?
I would prefer to not disable asp.net cache but wouldn't mind if there is no alternative. All the google hits for disabling cache didn't work though.
UPDATE
I would think the value of the inputs should correspond to the initial state of the entry in the history list.
This doesn't seem to be the case. It corresponds to the state right before post.
- http:\localhost:8080\index.html => input = ""
- http:\localhost:8080\index.html?val=2 => input = "2"
- http:\localhost:8080\index.html => input = "2"
An alternative is clearing out the fields with JavaScript when then document is done loading.
You could perhaps introduce a 'dummy' URL variable in order to trick the browser into believing it is a completely new page.
www.example.com/form.aspx?dummy=3423902
Use some javascript to make the browser pull the page again with a new random dummy=
The sever just igores this value.
I understand this will cause 2 postbacks each time but should certainly work. Work on 'asking' the browser to not cache first using the http headers - although I believe when you say:
I would prefer to not disable asp.net cache but wouldn't mind if there is no alternative. All the google hits for disabling cache didn't work though.
That this isn't working.
This should work. It's a jquery function so you would need to have that referenced in your page. As an example, this will set the value of the input item to "A"...
$(document).ready(function() {
// this code will be run immediately after the page has displayed.
// "ROpt" is the id of the field.
$("#ROpt").val("A");
});
来源:https://stackoverflow.com/questions/4594489/pressing-back-prefills-inputs-with-value-from-right-before-submit