简体   繁体   中英

Reset form only when clicking back button from SUCCESS landing page, NOT from ERROR page

I have a form.

Successful submission will load the success .html page.

If there is an error (empty field or invalid email address) it will load the error .html page.

I'd like to reset the form (empty all fields) only if a visitor presses the browser's back button from the success .html. If a visitor presses the BACK button on the error page the form should not be reset, ie the browser should behave as it normally does (default).

This code resets the form when somebody presses the back button to get back to the form:

$(window).bind("pageshow", function() {
  document.form_name.reset();
});

So how can I make the form page aware of where the visitor came back from (when people press the browser's back button)?

I thought I might define a variable (if true then reset, false on error page, true on success page) but for some reason this does not work. What is the best practice here?

Thank you for your help.

Ok, I think I worked it out:

on the success page I add one JS line:

top.name='success';

And on the page with the form I add this:

$(window).bind("pageshow", function() {
    if (top.name === "success") {
             document.form_name.reset();
                top.name="";}
});

It works for me if JavaScript is enabled. Should work in most browsers. Maybe not the most elegant solution + depends on JS being enabled but I couldn't think of any other that's working. I read that the window.name method is not secure (other pages can read the window.name) but then the word 'success' is not really highly classified information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM