Im using magnific popup for my site and found this script to create 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 code got executed before any input(button) is triggered.
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 stuck on that window.
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, wasnt able to use a on click event inside the when.
If i was able to pause id like to preventDefault when no is clicked else continue.
A call of this function could look like that:
<a href="de/site.html/del/46" onclick="confirmDialog('head', 'text', 'yes', 'no');"></a>
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.
Whats the best way to solve this problem ?
from Custom alert box, wait for input (yes / no) until resume javascript
No comments:
Post a Comment