Stop jQuery ignoring the if/else conditions

print
You may or may not have come across this.  If you are using an AJAX call and have added an if/else statement to the success: parameter, and in particular have bound some actions to an element within those clauses, it can carry on and run the else, even if the if has been satisfied.

Example:

success: function(result) {

            if(result == "success")
            {
                window.location.replace('/somewhere_else');
            }
            else {
                $("#errorMsg").append(result);
            }

        }

This can be avoided if you terminate the jQuery with a false return.

Example:

success: function(result) {

            if(result == "success")
            {
                window.location.replace('/portal');
                return false;
            }
            else {
                $("#errorMsg").append(result);
            }

        }

It will halt the jQuery in its tracks, before it can do something odd.

This is observational behaviour and could well be something that I am doing that is causing this in the first place, so if it is, and somebody would be kind enough to point it out, I would be very obliged.

Share this post:
Related Posts
laravel-framework-help-and-guide
Laravel Error: 1071 Specified key was too long
Disable and enable buttons (Bootstrap) using a jQuery extension

Leave a Comment