javascript 如何“自动关闭”警报框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17321574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:53:30  来源:igfitidea点击:

How to "auto close" Alert boxes

javascriptalertlegacy-code

提问by War

We have a control that was made by a company that no longer exists. For some odd reason on page load it has now started rendering something like this to the page:

我们有一个由一家不再存在的公司制作的控件。由于页面加载时的一些奇怪原因,它现在已经开始向页面呈现如下内容:

<script type="text/javascript">
 alert('Your license has expired!')
</script>

Since the company no longer exists we can't get support and the control is also very complex and is running in some legacy code that can't be quickly replaced so simply rewriting the page is also not an option (yet).

由于公司不再存在,我们无法获得支持,并且控件也非常复杂,并且运行在一些无法快速替换的遗留代码中,因此简单地重写页面也不是一种选择(目前)。

What I need to do for the time being is to have the dialog either be removed from the page before it renders or auto closed by some script ...

我暂时需要做的是让对话框在呈现之前从页面中删除或由某些脚本自动关闭......

Any ideas?

有任何想法吗?

回答by epascarello

You can not close an alert box, just you can hiHyman window.alert

您无法关闭警报框,只能劫持 window.alert

window._alert = window.alert;
window.alert = function () {    
};

The code would have to appear before the third party library's code. What this means is, if you want to use an alert, you would have to change your code.

该代码必须出现在第三方库的代码之前。这意味着,如果您想使用警报,则必须更改代码。

One way would to call the method that has the reference

一种方法是调用具有引用的方法

window._alert("hi");

Other way would be to overload the "new" function

另一种方法是重载“新”功能

window._alert = window.alert;
window.alert = function (msg, showItNow) {    
    if (showItNow) {
        window._alert(msg);
    }
};
window.alert("BOOOO!");  //I will not show up
window.alert("hi", true); //I will show up

回答by Madanlal Arora

I think you cannot do that because and alert box needs a confirmation.you can rather make a popup alert and set it to close with timeout.

我认为您不能这样做,因为警报框需要确认。您可以制作一个弹出式警报并将其设置为超时关闭。