Reset function alert

2

We use alert(text) to give an alert in the window, there is a way to redefine this function so instead of displaying this default window it displays a custom made in HTML but without losing its real function, it would just be an exchange of the " design "of the dialog box without losing its real functionality.

According to the answers I can even guide me, but how to program the part that returns OK / CANCEL / NO

Type, this is my job:

window.safeConfirm = function(params, callback) {
    if (typeof params === 'string') {
        params = {
            message: params
        };
    }
    var result = false;
    try {
        result = confirm(params.message);
    } catch (e) {
        result = true;
    }
    setTimeout(function() {
        callback(result);
    }, 10);
};

It serves to give an alert in the form of dialogue "yes / no" and perform a function according to the user's response, its use would be something like:

safeConfirm({
    type: 'ERROR_1',
    message: 'lalalala'
}, function() {
    window.location.reload();
});  

If I reset the function as in the replies this function would not work and I would lose what I want is the functionality of the alert with the alert being done in HTML .. So, anyway, how to do?

    
asked by anonymous 30.05.2015 / 04:59

3 answers

4

You can simply replace it:

/**
 * Ao invés de exibir uma janela de alerta,
 * a mensagem será exibida no console - CTRL + SHIFT + K.
 */
window.alert = function(msg){
  // faz algo
  console.log(msg);  
}

alert("StackOverflowPT");

If you want to "do something" and still maintain the behavior of alert , you can do this:

/**
 * Exibe a mensagem no console mas ainda assim
 * mantém o comportamento do 'alert'.
 */
var _alert = window.alert;

window.alert = function(msg) {
  // faz algo
  console.log(msg);
  return _alert.apply(this, arguments);
};

alert("StackOverflowPT");
    
30.05.2015 / 05:32
3

If you want to style the native window that appears for when the user is asked input , or the system alert window is not possible.

You can rewrite the alert function as @renan suggested but not the native Browser window for actions / standard behaviors. Example: link

This would in fact be a major security breach and could easily harm the user if it was misused.

    
30.05.2015 / 06:48
2

The functions alert , prompt and confirm , native to browsers, have behavior that is impossible to reproduce: they block the processing of any subsequent JavaScript code until they are dispensed. This is why prompt and confirm allow you to directly capture a return value, as in this example:

var valorDigitado = prompt('Digite um valor');

Any custom replacement (even if done using a modern HTML5% with% ) will be asynchronous and will depend on callbacks, as in your example and the Sergio example , and therefore could not replace directly a call to <dialog> or prompt , which are synchronous functions, which return a value.

    
02.06.2015 / 22:13