[removed] Overriding alert()

2018-12-31 05:45发布

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

  • Which browsers support this?
  • Which browser-versions support this?
  • What are the dangers in overriding the function?

11条回答
与君花间醉酒
2楼-- · 2018-12-31 06:11

I think every Javascript implementation will support this, and there is no danger involved with it. It's commonly done to replace the plain OS-styled alert boxes to something more elegant with HTML/CSS. Doing it this way means you don't have to change existing code! The fact that it is possible makes Javascript awesome.

查看更多
千与千寻千般痛.
3楼-- · 2018-12-31 06:12

All JavaScript implementations in modern browsers support overriding.

The dangers are quite simply, that you would drive other team members absolutely crazy by overriding commonly known functions such as alert().

So unless you are overriding functions as a tool to perhaps debug or hack existing code in some way, I don't see any reason to do it. Just create a new function.

查看更多
初与友歌
4楼-- · 2018-12-31 06:21

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 ;-)

查看更多
无与为乐者.
5楼-- · 2018-12-31 06:21

It sure works in firefox and ie8. I can't see that there'd be any browser it wouldn't work in. This is pretty much fundamental of how javascript works, even though one don't often see it used with native functions like that =)

查看更多
临风纵饮
6楼-- · 2018-12-31 06:21

A quick hack that I do to find where the alerts are coming from, is to go to console and then enter this

function alert(message) { 
  console.info(message);
  debugger;
} 
查看更多
十年一品温如言
7楼-- · 2018-12-31 06:22

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.

查看更多
登录 后发表回答