-1

I am trying to make a condition to my select input ..

$(document).on("change", "#action", function () {
    if(this.value !== "" ){ 
      location = this.value;
    }
});

but if the condition is false, it will just ignore and continue running the rest of the scripts ..

here's my remaining scripts:

$(document).on("click", "#delete_button", function () {
   $("#delete_modal").modal("show");
});


$(document).on("click", "#logout_button", function () {
   var url = $(this).data("url");
   $("#logout_form").attr("action", url);
   $("#logout_form").submit();
});

and here's my select;

<select id="action" class="form-control input-sm">
    <option value="">Select</option>
    <option value="/details/1">Details</option>
    <option value="edit/1">Edit</option>
    <option value="" data-url="/delete/1" id="delete_button">Delete</option>              
    <option value="" data-url="/logout/1" id="logout_button">Logout</option>
</select>

This code works in firefox and IE but doesn't work in chrome ..

How to fix this Problem??

BJ Langruto
  • 77
  • 2
  • 7

1 Answers1

0

Browsers can vary at which point they terminate execution when a new page load request (e.g. via location.href) is received.

To be sure of no further execution, stop it manually with a flag.

var terminate_execution = false; //<-- flag starts as false
$(document).on("change", "#action", function () {
    if(this.value !== "" ){ 
      terminate_execution = 1; //<-- set flag to true
      location = this.value;
    }
});

And then

$(document).on("click", "#delete_button", function () {
    if (terminate_execution) return false; //<-- check for flag first
    $("#delete_modal").modal("show");
});

$(document).on("click", "#logout_button", function () {
    if (terminate_execution) return false; //<-- check for flag first
    var url = $(this).data("url");
    $("#logout_form").attr("action", url);
    $("#logout_form").submit();
});
Mitya
  • 33,629
  • 9
  • 60
  • 107