Im using magnific popup for my site and found this script to create alert/confirm boxes:
var confirmDialog = function(headline, message, yes, no) {
var dialog = '<div class="dialog confirm mfp-with-anim white-popup-block white-popup-block2">';
if (headline) {
dialog += '<h2>' + headline + '</h2>';
}
dialog += '<div class="content"><p>' + message + '</p></div><hr/>';
dialog += ' <div class="product-action center">';
dialog += ' <button type="button" class="btn btn-primary btn-submit greenback">' + yes + '</button> ';
if(no){
dialog += ' <button type="button" class="btn btn-default btn-cancel">' + no + '</button>';
}
dialog += ' </div>';
dialog += '</div>';
$.magnificPopup.open({
modal: true,
items: {
src: dialog,
type: 'inline'
},
callbacks: {
open: function() {
var $content = $(this.content);
$content.on('click', '.btn-submit', function() {
$.magnificPopup.close();
});
$content.on('click', '.btn-cancel', function() {
event.preventDefault();
$.magnificPopup.close();
});
}
}
});
};
This code can be found orginal here: https://gist.github.com/sabl0r/e8f37147a2a263f4b860
The problem it does not work like a alert()
/confirm()
call, the event got executed before any input(button) is clicked.
I could use preventDefault()
to solve this problem but there is no way to resume the event after and i use this alert on many and very different ways. But this alert box get triggered always after the site is load already, so it wouldnt harm if the handler would stuck on that window waiting for response.
Is there any way to pause the Javascript handler and resume after a input button yes/no is triggered ?
First of all i came to the idea i could use a timer(setTimeout) wich is running infinity, but i guess thats not a good solution.
Next i came to the idea to use Jquery.when and done, but i wasnt able to get this working if its even possible.
If i was able to pause id like to preventDefault when no is clicked and continue the event if yes is clicked.
A call of this function could look like that:
<a href="de/site.html/del/46" onclick="confirmDialog('head', 'text', 'yes', 'no');"></a>
or
<form method="post" id="delform2" autocomplete="off" onsubmit="confirmDialog('head', 'text', 'Yes', 'No');">
<input id="btn_newsdel" name="btn_newsdel" type="submit" value="Newsletter sign out">
</form>
But as i said it can be very different. It would make no sense to attract the link and call that location after preventDefault and yes get triggered.
And submitting after preventDefault is called would be another problem wich wouldnt be easy to solve on second example.
Whats the best way to solve this problem ?