Search code examples
javascriptbootbox

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.alertin javascript function but don't work. How I can do that?


Solution

  • 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