Search code examples
javascriptalertlegacy-code

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?


Solution

  • 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