Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

javascript - JavaScript:覆盖alert()(JavaScript: Overriding alert())

Has anyone got any experience with overriding the alert() function in JavaScript?(有没有人有重写JavaScript中的alert()函数的经验?)

  • Which browsers support this?(哪些浏览器支持此功能?)
  • Which browser-versions support this?(哪些浏览器版本支持此功能?)
  • What are the dangers in overriding the function?(覆盖该功能有哪些危险?)
  ask by cllpse translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(更多信息在这里: jQuery类型#Proxy模式)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...