I\'m trying to use the jQuery alerts dialog library from http://abeautifulsite.net/notebook/87 instead of the default alerts (which look pretty awful in my opinion). This se
Think about callbacks as sending messages and it will result in better structure in your code.
Since the callback is asynchronous (at least, in the sense that it's waiting on the user to do something), it might be easier to handle what you need to inside the callback:
function confirm() {
jConfirm('are you sure?', 'Confirmation Dialog', function(r) {
if (r) doSomething();
});
}
@klogan [comments]
I assume you got these from here?
The page gives you your answer: (look under Usage)
These methods do not return the same values as confirm() and prompt(). You must access the resulting values using a callback function. (See the demo for more details.)
@klogan
The point I'm trying to make is that there isn't really an easy way to accomplish what you want. You're trying to correlate procedural and event-driven programming -- something JavaScript doesn't help you do.
The simplest (though, risky) solution is to use a pseudo-infinite-loop. But, if callback
never gets called, you now have an actual infinite loop. And, depending on the JavaScript engine, you might kill the browser waiting.
Point: Your best bet is to avoid this trying to force event-driven into procedural.
function confirm() {
var result = false;
var response = false;
jConfirm('are you sure?', 'Confirmation Dialog',
function(r) {
result = r;
response = true;
});
while(!response) continue; // wait
return result;
}
This thing works. Needs jQuery.
function myconfirm(kyssa, elm, e){ // neG
if(jQuery('#confirmquestion').data('result')){
var V = jQuery('#confirmquestion').data('result');
jQuery('#confirmquestion').remove();
return V == 'Y' ? true : false;
}else if(!jQuery('#confirmquestion').length){
jQuery('body').append('<div id="confirmquestion">'+
'<h4>Kinnitus</h4>'+
'<div id="kyssa">'+kyssa+'</div>'+
'<center>'+
'<button onclick="jQuery(\'#confirmquestion\').data(\'result\', \'Y\');">Jah</button> '+
'<button onclick="jQuery(\'#confirmquestion\').data(\'result\', \'N\');">Ei</button></div>'+
'</center>');
jQuery('#confirmquestion button').click(function(){
jQuery(elm).trigger(e.type);
})
}
return false;
}
onChange="if(myconfirm(\'Saada kiri: \'+jQuery(this).find(\'option:selected\').html()+\' ?\', this, event)) { ... }"
CSS
#confirmquestion{
border:1px solid #999;
background:white;
padding-bottom: 30px;
position:fixed;
width:300px;
font-size:12px;
top:45%;
left:50%;
margin-left:-150px;
}
#confirmquestion h4 {
background:blue;
color:white;
margin:0;
padding: 2px 5px;
border-bottom:#777;
text-align:center;
}
#confirmquestion #kyssa {
padding: 30px 25px;
}