Block form submission when click back

Asked

Viewed 1,353 times

1

It may seem like a stupid question but I’m amazed that when I click the back button, the form is sent.

The code is similar to this:

<form id="idform" method="post" onsubmit="return funcaodevalidacao(this);">
    <!-- conteúdo do form -->
    <input type="submit" value="Enviar"> <button onclick="window.location.href = 'URL'">Voltar</button>
</form>
<script>
    $('form').submit(function (ev) {
        ev.preventDefault();
        $(window).unbind('beforeunload');
        return false;
    });
</script>

What happens is the following: when I click on Back, which should redirect me to a desired URL, the form is SENT, and then is redirected to the URL, what I don’t understand is why the form is being sent.

I tried the way described above to use preventDefault, disable beforeunload, return false, but the form is still sent.

How do I block this behavior? I’m testing in firefox.

PS: the function: function Devaluation(this) is returning false for test questions. The palliative solution I found was to remove the 'form' element, but I believe this is not the correct alternative. I checked the link http://www.w3.org/TR/html401/interact/forms.html#h-17.12 that disabling the element is also not possible.

1 answer

0


In fact the form I was using in the above example adds a second call to Submit where it actually prevents form submission but still calls the validation function.

The most correct solution I thought about in this case was to add this script to the back button:

$('form').attr('onsubmit','return false;');window.location.href='URL'

This overrides the validation function that gave the impression that the form was being sent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.