问题
The title is actually a failed attempt to achieve my goal. Specifically, I want to
1. Go to a page from main.html (say, to page1.html).
2. Collect data in a form on page1.
3. On Submit, go to page2.html (which saves data to database using PHP and announces result).
4. Click on "Ok" and go back to main menu page.
Seems reasonable, right?
Step 4 above is my problem.
Things I've tried:
1. Invoke history.back on Ok click in page2.
This goes to page1 not main
2. Invoke javascript location.href=main.html on "Ok" in page2
This works and goes to main but clicking on Back button in main brings user back to page1. (not good)
3. Many variations of using javascript history.replace() in action= and onSubmit() of page1.
This works works and but inputs from form are not passed in query string to page2)
4. Clear history.
This appears to be impossible because of security concerns.
5. Programmatically composing the query string for history.replace (from form inputs) on page1.
This works but is very laborious, error-prone and horribly inelegant
Any suggestions will be greatly appreciated.
========= MY SOLUTION 4/8/16 ==============
I'm posting my final solution below for anybody that's interested.
In page1.html:
<script type="text/javascript">
function addarg(q,addend)
{
qnew = q + addend;
return( qnew );
}
function Next()
{
// GET INPUT VALUES FROM FORM
frm = document.forms["MainForm"];
first = frm.first.value;
last = frm.last.value;
phone = frm.phone.value;
// COMPOSE QUERY
query = "processForm.php";
query = addarg( query, '?first='+first );
query = addarg( query, '&last='+last );
query = addarg( query, '&phone='+phone );
location.replace( query ); // chain to processing page
}
</script>
<form action="javascript:Next()" name="MainForm" method=GET>
...
</form>
When history.back() is issued from processForm.php, it returns to Main Menu as desired.
回答1:
It seems the easier way to achieve this is to place a simple <a href=main.html>Ok</a> on your page2.html.
Then associate a flag (maybe a cookie) on this main.html which "instructs" page1.html to clear its form elements if it is not called by main.html, but from a "back" button (by refreshing meta, for example, or by loading page1.html#randomnum, where randomnum is a value you find real time to force your browser understand this is not the "old" page1.html but a "new" page1.html, helping browser not to get this from cache but from web).
The principle is: if page1.html is called by "back", it have to be a "clear" page1.html, not showing any data input user eventually entered.
来源:https://stackoverflow.com/questions/36429982/how-to-do-html-form-action-location-replacenextpage-html