Is there any way to dismiss JavaScript alert() programatically OR by pressing any key and not necessarily pressing the ENTER key?
            Asked
            
        
        
            Active
            
        
            Viewed 149 times
        
    0
            
            
        
        AndrewL64
        
- 15,794
 - 8
 - 47
 - 79
 
        John Lewis
        
- 1
 - 1
 
- 
                    1ALL JavaScript is blocked until the alert continues – Sebastian Speitel Nov 29 '18 at 12:12
 - 
                    No. Alert is synchronous and can only be operated on by the user. – Phiter Nov 29 '18 at 12:12
 - 
                    2You shouldn't use alert on a real application in most cases. Try an alternative like sweet alert – Phiter Nov 29 '18 at 12:13
 - 
                    use setTimeout() – p u Nov 29 '18 at 12:13
 
2 Answers
2
            
            
        You may try boot box plugin
var alert = bootbox.alert('Count to 5...');
alert.show();
setTimeout(function() { alert.modal('hide'); }, 5000);
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js"></script>
- 
                    I took the liberty to change it to a runnable snippet with CDN links :-) – Peter B Nov 29 '18 at 12:32
 - 
                    
 
0
            
            
        Yes, you can use ESC key instead of Enter
        Sebastian Kaczmarek
        
- 8,120
 - 4
 - 20
 - 38
 
- 
                    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/21550125) – Mustapha Larhrouch Nov 29 '18 at 14:15
 - 
                    I must disagree, it does provide an answer to the question. It provides *direct* answer to the question. Let me quote OP: "Is there any way to dismiss [...] alert() programatically *OR* by pressing any key and not necessarily pressing the ENTER key?". And the answer is - yes, you can dismiss `alert()` by pressing any other key than enter - the key is ESC. And this is what I suggested – Sebastian Kaczmarek Dec 01 '18 at 12:37