[removed] Overriding alert()

后端 未结 12 2068
忘了有多久
忘了有多久 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:47

    My experience with overriding alert() function is that we once used it to "hack" trial version of JavaScript library that displayed "Please register!" nag screen through alert time to time.

    We just defined our own alert() function and voila.

    It was for testing purposes only, we bought full version later, so nothing immoral going on here ;-)

    0 讨论(0)
  • 2020-11-22 03:54

    Ladislav.
    For IE8 you can redefine alert() like this way

    /** 
     * Definition of global attached to window properties <br/>
     */ 
        (function() {
          nalert = window.alert;
          Type = {
              native: 'native',
              custom: 'custom'
          };
        })();
    
    /**
     * Factory method for calling alert(). 
     * It will be call a native alert() or a custom redefined alert() by a Type param.
     * This defeinition need for IE
     */ 
        (function(proxy) {
    
              proxy.alert = function () {
              var message = (!arguments[0]) ? 'null': arguments[0];
              var type = (!arguments[1]) ? '': arguments[1];
    
              if(type && type == 'native') {
               nalert(message);
              }
              else {
                   document.write('<h1>I am redefiend alert()<br/>Alert say: '+message+'</h1>');
              }     
          };
       })(this);
    

    and call as

    alert('Hello, hacker!');
    nalert('I am native alert');
    alert('Hello, user!', Type.custom);
    
    0 讨论(0)
  • 2020-11-22 03:56

    It's definitely "supported". It is your web page, you do whatever you want to with it.

    I already did this to track analytics events without modifying a library but by sneaking into events.

    Use the proxy pattern:

    (function(proxied) {
      window.alert = function() {
        // do something here
        return proxied.apply(this, arguments);
      };
    })(window.alert);
    

    You can also bypass the call to the original function if you want (proxied)

    More info here: JQuery Types #Proxy Pattern

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 03:59

    As said in many of the other answers, you can just override the function with

    window.alert = null
    

    or

    window.alert = function(){}
    

    however, this doesn't necessarily override the function on the prototype of the Window constructor (note the capital W), so the hacker can still type:

    Window.prototype.alert.apply(window, ["You were hacked!"]);
    

    therefore, you also need to override that function with:

    Window.prototype.alert = null
    

    or

    Window.prototype.alert = function(){}
    
    0 讨论(0)
  • 2020-11-22 04:00

    When it comes to js browser functions window.alert is pre-eminent and most well known, people who don't know js know alert() -- rest assured it is supported in all browsers in use today and your code snippet is as well. However, I wouldn't override (well this is more like refactoring rather than override in the OOP sense) alert() for a particular use case as yours because when you actually need to use alert() with no template, and you probably will, then you'll need another non-alert function to do so.

    0 讨论(0)
提交回复
热议问题