Correct Cancel Button Logic for JS

The Two approved methods for cancel buttons. The onclick will eventually be removed from the Submit button as well.

by ritcheyer

HTML

<!-- // Method 1 - use when you need to clear the user's session // -->
<div class="btn-row">
    <button id="btnSubmit" class="btn btn-primary" onclick="return btnSubmit_onclick()">Submit</button>
    <a href="/app/" class="btn-helper-text evtBtnCancel" title="Cancel and Return to App Home">Cancel</a>
</div>

<!-- // Method 2 - when you don't need to clear the user's session // -->
<div class="btn-row">
    <button id="btnSubmit" class="btn btn-primary" onclick="return btnSubmit_onclick()">Submit</button>
    <a href="/app/" class="btn-helper-text" title="Cancel and Return to App Home">Cancel</a>
</div>

JavaScript

// approved method 1 (when you need to clear the session)
$(document).ready(function(){
    $('.evtBtnCancel').click(function(e){
        e.preventDefault();                           // hijack the click
        $.postJSONSync('/app/service/clear_session'); // clear session
        window.location = '/app/';                    // redirect user
    });
});

// approved method 2
// no js needed for method 2