Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - How to "auto close" Alert boxes

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?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can not close an alert box, just you can hijack 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...