0

Here is what I'm trying to do....

function myFunction()
{
  var x=jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
      y=r; 
      alert(y);
  });

  alert(x);
 }

Why does alert(x) runs first(I'm beginner in javascript) and it alerts "undefined", and after I press Ok it alerts "True", and "False" when I press Cancel..?

Is there any way I can make jConfirm return true or false...ie x to be trur or false?

I'm using the "http://abeautifulsite.net/" plugin for jConfirm.

dividedbyzero
  • 187
  • 3
  • 16

1 Answers1

0

jConfirm doesn't create alert boxes, it creates a HTML element which looks like an alert box. The important difference here is that an alert() box will stop Javascript execution until it's closed, so alert(1); console.log(2); will alert first, then log to the console. Since jConfirm doesn't use alert(), if you call jConfirm(1, ...); console.log(2) it will show the confirm box (HTML), then write to the console a split-second later.

The complicated-words version of this is that alert() is synchronous - it locks the script execution until you've chosen something from the alert. jConfirm is asynchronous, and the script execution continues independently of your choice until you choose something, at which point it triggers a callback function.

Joe
  • 15,669
  • 4
  • 48
  • 83
  • There must be some way of making jConfirm synchronous?...actually the above code is not the real problem..what I have is a delete icon, when the users clicks on delete, jConfirm should ask for confirmation and based on the option clicked action should be performed. – dividedbyzero Sep 23 '14 at 10:48
  • 2
    Why must there be? It was written to be asynchronous and it's not something you can quickly change. You'll probably have to modify your code to work on callbacks and make it **event-driven** (Google it). You'll have to make a function called `deleteRow` (or something similar) and call it from the jConfirm callback if they clicked "yes" – Joe Sep 23 '14 at 10:51
  • Why can't you put the action code in the callback? That's what it's meant for. – JJJ Sep 23 '14 at 10:57
  • @Juhana "You'll have to" may have been a touch misleading - the code for deleting a row **has** to be invoked from the callback, but doesn't necessarily need to call a separate function. That said, sure you can just inline it into the callback, but if it's the code to delete a row, you may as well put it into the `deleteRow` function for maintainability – Joe Sep 23 '14 at 11:01
  • Sorry, that comment was to @dividedbyzero, not you. – JJJ Sep 23 '14 at 11:06
  • Ah, fair enough :) I must just be too fast at typing replies :P – Joe Sep 23 '14 at 11:08
  • Googling "Javascript callback tutorial" might help, here's the first result :-) http://recurial.com/programming/understanding-callback-functions-in-javascript/ – Joe Sep 23 '14 at 11:30
  • @joe ......what have i done now is ... jConfirm('@SystemMessages.INFO_DeleteMessages',"Confirm",function(r){ if(r) { alert(r); alert("Why"); //Code to delete; } }); Now when i Press ok after alerting true and why it alerts me [object Object]....any idea why so...the same code worked fine under confirm – dividedbyzero Sep 23 '14 at 11:52
  • It doesn't delete it...but I think Im close, any idea what might be wrong?...here my jConfirm plugin code jConfirm = function(message, title, callback) { $.alerts.confirm(message, title, callback); }; Any guess if i have to change something here – dividedbyzero Sep 23 '14 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61752/discussion-between-dividedbyzero-and-joe). – dividedbyzero Sep 23 '14 at 12:03
  • I would, but you ain't in the chatroom :/ – Joe Sep 23 '14 at 12:18