Implement a rule in javascript if user clicks cancel on prompt

  • 1
  • 1
  • Question
  • Updated 6 months ago
  • Answered
Is there a way to implement a rule in javascript which will cancel running the code if user clicks cancel on a prompt window within the code?

Thanks
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb

Posted 6 months ago

  • 1
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,532 Points 20k badge 2x thumb
Something as simple as this will work:
if(confirm("Do you want to continue?")) {
  //run code
}
If you want a better dialog box and more options used jQueryUI Dialog as shown in this demo:

https://haversineconsulting.quickbase.com/db/bkw2ff3e3?a=q&qid=1
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
Thanks Dan!

I am doing this in myCript page.

I have a prompt:

var description= prompt("Enter a brief description of the work done in this session: ", "");

if the user cancels within the prompt window then don't want to run the code. I tried putting the if statement but it does not seem to work:

See in bold what I added

(function(){
  var dbid = "bnqepesti";
  var dbidTable = dbid;
  var apptoken = "my app token";
  $.ajaxSetup({data: {apptoken: apptoken}});
  var description= prompt("Enter a brief description of the work done in this session: ", "");
  if(confirm("Do you want to continue?")) {
  var promise = $.get(dbidTable, {
    act: "API_EditRecord",
    rid: QBU_rid,
   _fid_20: description,
    _fid_6: "Out"
  }).then(function(xml) {
    console.dirxml(xml);
 $.get() & location.reload(true);
  });
})();}
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
I figured this out:

I entered these lines of code after the prompt line:

  if (description == null || description == "")  {
  alert("You did not enter a description I will cancel this Time Out.");
  Return;
}

This will alert and cancel the rest of the code if the user clicks cancel or leaves it blank and clicks ok.

I hope this can help others interested in implementing this in their apps.