KDD
KDD

Reputation: 75

window.onbeforeunload with a form

I will try to explain this correctly:

I have a website with a huge form on it. This form has a ton of fields and it all run by another JS file.

To be exact: in my form I have text box's and drop down menus. I want to be able to "reset/refresh" my page after someone leaves it. There's a button at the bottom of the foam that takes you away from the site into a shopping cart.

After going to the shopping cart I want to be able to click the back button and have all the fields reset back to the original state. I tried putting this near the top but it's having no effect. Maybe i'm sticking it in the wrong place?:

<script type="text/javascript">
window.onbeforeunload = function () {
    }
   </script>

Upvotes: 1

Views: 220

Answers (1)

mellamokb
mellamokb

Reputation: 56779

You can try resetting the form in the onload event, which will happen on first load of the form, and also when the user navigates back to the form with the back button:

window.onload = function() {
    var frm = document.form1;   // put form name here
    for (var i = 0; i < frm.elements.length; i++)
        frm.elements[i].value = "";
};

Demo: http://jsfiddle.net/bavRd/

Upvotes: 1

Related Questions