Reset the Alert function

Asked

Viewed 867 times

2

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

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

Like, this job of mine:

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 "yes/no" dialog 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 responses this function would not work and I would lose what I want is Alert functionality with Alert being done in HTML.. So after all, how to do?

3 answers

4

You can just 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, can do so:

/**
 * 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");

  • Renan, this this refers to the _alert and arguments refers to the msg? So in practice, it’s like you’re doing something like: var a = function(msg) { console.log(msg); return alert(msg); };, only by keeping the name alert to the function? Just to be sure! : P

  • Yes I wanted to keep the Alert function but not it in its entirety (displaying the window...) please see my updated question please?

  • @Renan really I shuffled.. I’ll have to resort to a plugin

3

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

You can rewrite the function alert as @Renan suggested but not the native browser window for actions/behaviors standard. Example: http://jsfiddle.net/rsLavcm5/

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

  • Can you provide an example of a security breach? I certainly agree - perhaps out of inertia or perhaps because it preserves the basis of the language and avoids problems in the code - but I don’t think about anything. Security is not my strong suit.

  • 1

    @If it were possible to change the native alerts of the browser it would be possible for example to ignore all alerts that the Browser gives, as well as to manipulate all requests for "agree / do not agree" that the browser asks about cookies, credit card, etc...

  • Sergio I adapted my question and detailed it.

  • @user3163662 what you want is to replace the native window for type confirmations OK/ Cancel?

  • @Sergio would be so much for this so much for just a normal Alert..

  • @user3163662 which is native to Browser cannot replace. It is protected by Browser.

  • @Sergio but can’t make a script that works similarly?

  • 1

    @user3163662 no.

  • @Sergio has to have Uai JS does so much why not one that works like Alert.. But Thank you! I’ll think of something and answer the question if necessary.

Show 4 more comments

2

The functions alert, prompt and confirm, native to browsers, they have a behavior that is impossible to reproduce: they block the processing of any later Javascript code until they are dispensed with. That’s 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 <dialog>) will be asynchronous and will depend on callbacks, as in your example and example of Sergio, and therefore could not directly replace a call to prompt or confirm, which are synchronous functions, which return a value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.