简体   繁体   中英

Call Bootbox.js from Javascript function

I need way to run bootbox I have next code:

<script>
function validateForm() {
var x1 = document.forms["contactform"]["name"].value;
if (x1 == null || x1 == "") {
       alert("Please Enter name"); // I WANT CALL Bootboxjs HERE
        return false;
    }
}
 </script>

So I want replace standard javascript alert with this:

bootbox.alert("Hello world!", function() {
  Example.show("Hello world callback");
});

I tried to paste botbox.alert in javascript function but don't work. How I can do that?

What you want to achieve is replace the default javascript alert method

This feature is available in all browser without any kind of problem, just override the alert method with

window.alert = function(message){
    bootbox.alert(message, function() {
         // execute a predefined callback
    });
}
// and call like 

alert("Hello world"); // shows bootbox alert

and if you want to include the callback :

// include this before any alert call is executed in any script
window.alert = function(message,callback){
    bootbox.alert(message, callback);
}
// and call like 

alert("Hello world",function(){
  console.log("custom callback");
}); // shows bootbox alert

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM