[removed] Overriding alert()

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

提交回复
热议问题