欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

覆盖window.alert

程序员文章站 2022-06-14 10:23:26
...

For years the only bit of feedback web developers could get was via alert("{str}") calls. These days we have the web console but, in rare cases, we don't have a console and alert calls are our only window into a value at a given time.

多年来,Web开发人员只能通过alert("{str}")调用获得反馈。 如今,我们拥有Web控制台,但在极少数情况下,我们没有控制台,并且alert呼叫是我们在给定时间访问值的唯一窗口。

One problem: if an alert sneaks into production code, your site looks like it's been hacked. Your site looks like it's malware! To prevent any of those issues, you can add this snippet to your production build:

一个问题:如果alert潜入生产代码中,则您的网站看起来像被黑了。 您的网站看起来像是恶意软件! 为防止出现任何这些问题,您可以将此代码段添加到生产版本中:

window.alert = console.log

This tiny line of JavaScript could save your site from catastrophe. There are many cases for overriding native functionality and this is a great example!

这行很小JavaScript可以使您的网站免于灾难。 在许多情况下,本机功能都将被覆盖,这是一个很好的例子!

翻译自: https://davidwalsh.name/override-window-alert