JavaScript:重写alert()
有没有人有过重写 JavaScript 中的 alert()
函数的经验?
- 哪些浏览器支持这个?
- 哪些浏览器版本支持此功能?
- 重写该函数有什么危险?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
绝对是“支持”的。这是您的网页,您可以用它做任何您想做的事情。
我已经这样做了,在不修改库的情况下通过潜入事件来跟踪分析事件。
使用代理模式:
如果需要(代理),您还可以绕过对原始函数的调用
更多信息:JQuery 类型#代理模式
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:
You can also bypass the call to the original function if you want (proxied)
More info here: JQuery Types #Proxy Pattern
尽管大多数浏览器都支持覆盖它,但要小心您使用它所做的事情。
由于默认警报框会阻止执行线程,因此某些依赖此行为的库可能不再工作(最多)。
您应该成为一个好公民,避免接触本机 API。如果这样做,则在使用第 3 方代码时可能会破坏事情。
然而,如果您想在特定上下文中重新定义警报行为,您可以用匿名函数将其括起来,如下所示:
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:
超响报警功能不会产生任何危险。每个浏览器都支持它。
例如:
There are no dangers in Overring alert function. Every browser supprts it.
for example:
正如许多其他答案中所述,您可以使用
或
覆盖该函数,但是,这不一定覆盖
Window
构造函数原型上的函数(请注意大写的W< /code>),因此黑客仍然可以输入:
因此,您还需要使用以下内容覆盖该函数:
或
As said in many of the other answers, you can just override the function with
or
however, this doesn't necessarily override the function on the prototype of the
Window
constructor (note the capitalW
), so the hacker can still type:therefore, you also need to override that function with:
or
我认为每个 Javascript 实现都会支持这一点,并且不会有任何危险。通常将简单的操作系统风格的警报框替换为更优雅的 HTML/CSS。这样做意味着您不必更改现有代码!事实上,它是可能的,这使得 Javascript 变得很棒。
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.
拉迪斯拉夫。
对于IE8,你可以像这样重新定义alert()
并调用
Ladislav.
For IE8 you can redefine alert() like this way
and call as
我对重写alert()函数的经验是,我们曾经用它来“破解”显示“请注册!”的JavaScript库的试用版。时不时地通过警报打扰屏幕。
我们刚刚定义了自己的alert()函数,瞧。
它仅用于测试目的,我们后来购买了完整版本,所以这里没有发生任何不道德的事情;-)
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 ;-)
现代浏览器中的所有 JavaScript 实现都支持覆盖。
危险很简单,你会通过覆盖众所周知的函数(例如alert())让其他团队成员彻底疯狂。
因此,除非您以某种方式重写函数作为工具来调试或破解现有代码,否则我认为没有任何理由这样做。只需创建一个新函数即可。
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.
我面临着显示默认消息以及实际警报消息的要求。这就是我设法做到的。
我已经在 chrome 上测试过了。
我不确定这是否是一个好的做法,但它达到了目的。
I have faced a requirement to show a default message along with the actual alert messages. This is how I managed to do it.
I have tested it on chrome.
I am not sure whether its a good practise, but it serves the purpose.
它确实可以在 firefox 和 ie8 中运行。我看不出有任何浏览器它不能工作。这几乎是 javascript 工作原理的基础,尽管人们不经常看到它与这样的本机函数一起使用 =)
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 =)
说到 js 浏览器函数,
window.alert
是最杰出、最知名的,不懂 js 的人都知道alert()
——放心吧现在使用的所有浏览器都支持,您的代码片段也是如此。但是,我不会重写(这更像是重构,而不是 OOP 意义上的重写)alert()
对于您的特定用例,因为当您实际上需要使用alert ()
没有模板,您可能会这样做,那么您将需要另一个非警报函数来执行此操作。When it comes to js browser functions
window.alert
is pre-eminent and most well known, people who don't know js knowalert()
-- 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 usealert()
with no template, and you probably will, then you'll need another non-alert function to do so.我用来查找警报来源的一个快速方法是转到控制台,然后输入以下内容
A quick hack that I do to find where the alerts are coming from, is to go to console and then enter this