jquery - Replace standard javascript confirm with Twitter Bootstrap's modal - who triggered it? -
i'm trying replace standard javascript confirm twitter bootstrap modal window. working (the modal shown confirm text), i'm stuck trying catch caller href, need bind "ok" button.
here's code (almost working example: http://jsfiddle.net/k5udw/):
jquery.altconfirm = function (options) { var box = '<div class="modal fade static" id="confirm" data-backdrop="static" tabindex="-1" role="dialog">'; box += '<div class="modal-dialog">'; box += '<div class="modal-content">'; box += '<div class="modal-body"> </div>'; box += '<div class="modal-footer">'; box += '<button type="button" class="btn btn-default" data-dismiss="modal">no</button>'; box += '<button type="button" class="btn btn-primary">ok</button>'; box += '</div>'; box += '</div>'; box += '</div>'; box += '</div>'; $("body").append(box); window.confirm = function () { $(".modal-body").html( arguments[0].replace(/\n/, "<br />") ); $('.modal').modal(); $(".btn-default").on('click', function() { $(this).modal('hide'); }); $(".btn-primary").on('click', function() { return true; // need caller's href, // make proceed. // return true, obviously, nothing. }); }; }; $(document).ready(function() { $.altconfirm(); });
any hint? please note drop-in replacement standard javascript confirm, modifying way confirm called not possibility (if plugin active -> modal shown, if plugin not active -> standard confirm fired).
i've updated fiddle solution rewrites code on fly; useful solution if code generated framework , can't/don't want change framework's function writes it:
on document ready(), looks tags , if finds "confirm" call, update parameter passed in order store other informations (link open or action execute when ok pressed on modal); function overrides standard confirm(), returns false (to stop execution) , handle has done when user press ok on modal:
$(document).ready(function() { console.log('loading done'); jquery.each($('body').find('a'), function(i, val) { var hrefattr = ($(val).attr('href')); $(val).attr('href', ''); $(val).attr('onclick', function(index, value) { if (value != undefined) { var att = '-'; if (hrefattr == '#') { att = $(this).attr('onclick'); //att = att.match(/{ (.*) }/); att = att.substring(att.indexof('{') + 1, att.indexof('}')); } if (value.indexof("confirm('") >= 0) { return value.replace("confirm('", "confirm('" + hrefattr + '||| ' + att + '||| '); } if (value.indexof('confirm("') >= 0) { return value.replace('confirm("', 'confirm("' + hrefattr + '||| ' + att + '||| '); } } }); }); $.altconfirm(); });
Comments
Post a Comment