views:

8005

answers:

6

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.

A: 

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
+3  A: 

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
This makes me sad.
Andrew
Now... I'll admit I still do it from time to time but... really? Alert for debugging? You're *advocating* that?
eyelidlessness
A: 

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

Perpetualcoder
jGrowl simply displays an inline div. I need a dialog box. Thanks though.
Andrew
+1  A: 

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
My alert box would be fired by an asynchronous event, so this should not be a problem (in theory).
Andrew
JavaScript isn't asynchronous. Calling alert/confirm/prompt freezes all script processing (and indeed often the entire web browser) until the user answers.
bobince
A: 

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
That is not what I want. As I already said: I don't want an alert within the window (i.e. simply displaying a special div). I need a separate window for my purposes.
Andrew
I know, but unfortunately, it's really the only way to handle this stuff in JavaScript.
Toby Hede
It doesn't handle it. It doesn't solve my problem. I believe my question was very clear about what I needed. I specifically said that your solution was not acceptable in the question.
Andrew
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
You were telling me something I already knew and stated in the question as unacceptable. I didn't mean to be rude. I just wanted to make sure I was clear. Apparently, I tried too hard. My apologies if I came off as abrasive.
Andrew
All good - just a lack of context and tone in comments.
Toby Hede
+3  A: 

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
Exactly. You've summed up my entire problem perfectly.
Andrew