44

I want to be able to close an alert box automatically using Javascript after a certain amount of time or on a specific event (i.e. onkeypress). From my research, it doesn't look like that's possible with the built-in alert() function. Is there a way to override it and have control over the dialog box that it opens?

Also, I don't want an override that shows a hidden div as the alert. I need an actual dialog box.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Andrew Ensley
  • 11,611
  • 16
  • 61
  • 73

11 Answers11

24

As mentioned previously you really can't do this. You can do a modal dialog inside the window using a UI framework, or you can have a popup window, with a script that auto-closes after a timeout... each has a negative aspect. The modal window inside the browser won't create any notification if the window is minimized, and a programmatic (timer based) popup is likely to be blocked by modern browsers, and popup blockers.

Tracker1
  • 19,103
  • 12
  • 80
  • 106
11

Appears you can somewhat accomplish something similar with the Notification API. You can't control how long it stays visible (probably an OS preference of some kind--unless you specify requireInteraction true, then it stays up forever or until dismissed or until you close it), and it requires the user to click "allow notifications" (unfortunately) first, but here it is:

If you want it to close after 1s (all OS's leave it open 1s at least):

var notification = new Notification("Hi there!", {body: "some text"});
setTimeout(function() {notification.close()}, 1000);

If you wanted to show it longer than the "default" you could bind to the onclose callback and show another repeat notification I suppose, to replace it.

Ref: inspired by this answer, though that answer doesn't work in modern Chrome anymore, but the Notification API does.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • The requireinteraction flag is also "supposed" to make it stay up indefinitely (until they close it or you do with the #close method) FWIW (on OS .X default timeout is 5s FWIW) – rogerdpack Jan 05 '18 at 21:41
7

no control over the dialog box, if you had control over the dialog box you could write obtrusive javascript code. (Its is not a good idea to use alert for anything except debugging)

ForYourOwnGood
  • 38,822
  • 5
  • 30
  • 27
  • 17
    Now... I'll admit I still do it from time to time but... really? Alert for debugging? You're *advocating* that? – eyelidlessness Sep 24 '09 at 06:51
  • 6
    `alert()` is perfect for debugging. Last time I used it during writing userjs on my iPad @ office. God bless alert()! :) – A.D. Mar 10 '15 at 06:59
  • 4
    I find both alert and console.log to be helpful in different situations. Never use `alert();` within a loop though. Nice way to crash a computer :) – www139 Apr 07 '18 at 04:52
4

I want to be able to close an alert box automatically using javascript after a certain amount of time or on a specific event (i.e. onkeypress)

A sidenote: if you have an Alert("data"), you won't be able to keep code running in background (AFAIK)... . the dialog box is a modal window, so you can't lose focus too. So you won't have any keypress or timer running...

Ironicnet
  • 547
  • 3
  • 14
  • 1
    My alert box would be fired by an asynchronous event, so this should not be a problem (in theory). – Andrew Ensley Jan 20 '09 at 22:30
  • 5
    JavaScript isn't asynchronous. Calling alert/confirm/prompt freezes all script processing (and indeed often the entire web browser) until the user answers. – bobince Jan 20 '09 at 23:10
  • 2
    @bobince JavaScript does allow *asynchronous* behavior; but it is not *multithreaded*. If the thread freezes in a block of code --- asynchronous or not --- all code will be frozen at that point. – jpaugh Jul 23 '15 at 18:42
3

Try boot box plugin.

var alert = bootbox.alert('Massage')
alert.show();
setTimeout(function(){alert.modal('hide'); }, 4000);
2

I guess you could open a popup window and call that a dialog box. I'm unsure of the details, but I'm pretty sure you can close a window programmatically that you opened from javascript. Would this suffice?

David Hanak
  • 10,754
  • 3
  • 31
  • 39
  • 1
    Nice work around, but OP wanted alert dialog only. But this should do for those who want control over dialogs. +1 – GoodSp33d May 30 '13 at 11:55
2

The only real alternative here is to use some sort of custom widget with a modal option. Have a look at jQuery UI for an example of a dialog with these features. Similar things exist in just about every JS framework you can mention.

Toby Hede
  • 36,755
  • 28
  • 133
  • 162
  • 1
    I know, but unfortunately, it's really the only way to handle this stuff in JavaScript. – Toby Hede Jan 21 '09 at 03:41
  • 6
    No need to get annoyed, you asked a question and I said it couldn't be done, which it can't, and offered an alternative. – Toby Hede Jan 22 '09 at 05:50
  • 2
    Wow... years later, I can't believe how much of an idiot I was. Sorry I was such an ass when all you were trying to do was help. – Andrew Ensley Feb 14 '12 at 18:42
0

If you do it programmatically in JS it will be like reinventing the wheel. I recommend using a jQuery plugin called jGrowl

Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
-2

You actually can do this you can basically listen for this pop up to happen and then confirm true before it ever "pops up",

if(window.alert){
  return true
}
slfan
  • 8,950
  • 115
  • 65
  • 78
-3

You can use label and set its fade in and out time for e.g Hide it initially and show on click. $('#div_Message').fadeIn(500).delay(1000).fadeOut(1500);

Hammer
  • 7
-5
window.setTimeout('alert("Message goes here");window.close();', 5000);
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49