[removed] Overriding alert()

后端 未结 12 2088
忘了有多久
忘了有多久 2020-11-22 03:17

Has anyone got any experience with overriding the alert() function in JavaScript?

  • Which browsers support this?
  • Which browser-versions sup
12条回答
  •  心在旅途
    2020-11-22 03:59

    Although most browsers support overriding it, be careful with what you're doing with it.

    Since the default alert box blocks the execution thread, some libraries that rely on this behaviour might not work anymore (at best).

    You should be a good citizen and avoid touching the native API. If you do, you could break things up, when using 3rd party code.

    Yet, if you want to redefine the alert behaviour in a specific context, you could enclose it with an anonymous function, like this:

    /* new funky alert */
    function myFunkyAlert(msg) { 
        /* here goes your funky alert implementation */
        alert("Look ma!\n" + msg);
    }
    
    (function(alert) { // anonymous function redefining the "alert"
    
        /* sample code */
        alert("Hello World!");
    
    })(myFunkyAlert);
    

提交回复
热议问题