On one side I have a form with submit/cancel buttons and the cancel has an onclick event (to do some cleanup) that is wrapped in a standard confirm dialog. All standard stuff.
But then I have a second script (and it MUST be a second script) that may only sometimes be loaded, that needs to add more tasks if the form is cancelled. So - I can listen for the onclick...
$( "#cancelBtn" ).on( "click", function(a) {
// do more stuff...
});
but I only want to 'do more stuff' if the confirm around the onclick is OK. I am probably missing something stupidly simple but I have been at this all day and am about to give up! Can anyone point me in the right direction?
Update: I can not change the form onclick code - it has to stay separate and as it now is.... so the confirm has to stay where it is.
Do it like this:
$( "#cancelBtn" ).on( "click", function(a) {
if (confirm('Are you sure you want to cancel?')) { // Standard confirmation message.
// Pressed OK.
// do more stuff...
} else {
// Pressed Cancel.
}
});
Hope this helps.
EDIT: Since you can't change the script performing form duty, a solution would be to overwrite the confirm()
-function:
var _confirm = window.confirm;
window.confirm = function() {
//catch result
var confirmed = _confirm.apply(window,arguments);
if (confirmed) {
//confirm ok, do some tasks
} else {
//confirm cancelled, do some cleanup
}
//pass result on to the caller
return confirmed;
};
put the tasks in the second script in
function doMoreStuff(){
//more tasks
}
and call it in your confirm-clause like this
if (typeof doMoreStuff === "function") {
doMoreStuff();
}
or like this
try {
doMoreStuff();
}
catch(err) {
//no second script loaded
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.