1

On one of my pages, I need to make sure that the user has printed the page before continuing, so I intercept the button click with a simple onclick method, this prompts the user to confirm if they have printed the page or not.

If they have printed the page, I want to continue and do the postback, however if the user presses cancel, I expect the user to stay on the current page. This is not happening, currently even when pressing cancel, the page is still going back to the server, and the user is shown the next screen.

Button:

<button type="submit" name="Continue" value="Continue" class="button" onclick="clientClick();">Continue</button>

Javascript function to alert the user:

function clientClick() {
        if (confirm("MESSAGE")) {
            return true;
        }
        return false;
    }
Lex
  • 879
  • 3
  • 16
  • 27
  • 1
    Listening to the form's onsubmit attribute would be a better fit. See: http://stackoverflow.com/a/4227096/4270650 – Will Ray Oct 14 '15 at 14:20

2 Answers2

5
onclick="return clientClick();"

Consider the code you put as value of onclick attribute as the content of an anonymous function:

onclick="(function(){ return clientClick(); })();"
Igor
  • 15,833
  • 1
  • 27
  • 32
  • Thank you, Its been so long since I have done any JS, that I completely forgot to add return to it. – Lex Oct 14 '15 at 15:10
0

instead of type="submit" set type="button"

and in on click function

function clientClick() {
    if (confirm("MESSAGE")) {    $("#your_form_id").submit();
    }
    return false;
}
shayah
  • 39
  • 7