Is there a JavaScript alert that doesn't pause the script?

I'm looking for something like alert(), but that doesn't "pause" the script.

I want to display an alert and allow the next command, a form submit(), to continue. So the page will be changing after the alert is displayed, but it won't wait till the user has clicked OK.

Is there something like this or is it just one of those impossible things?


Solution 1:

You could do the alert in a setTimeout (which a very short timeout) as setTimeout is asynchronous:

setTimeout("alert('hello world');", 1);

Or to do it properly you really show use a method rather than a string into your setTimeout:

setTimeout(function() { alert('hello world'); }, 1);

Otherwise you open yourself up to JavaScript injection attacks. When you pass a string it is run through the JavaScript eval function.

Solution 2:

If already using JQuery http://docs.jquery.com/UI/Dialog is simple to use and styles nicely.

Solution 3:

It may not be what you're looking for, but it might be appropriate to use window.status = 'foo'.

I use this a lot in one of my webapps for a intranet. Also the setTimeout approach works too, but it can block if the browser is busy on an intensive task. However the status bar update is always immediate.

This does require your viewers to have the setting that javascript can change the status bar -- always the case if it's a trusted site.

Solution 4:

In this case, it would be more appropriate to use DHTML and JavaScript to dynamically display a message, either in a plain HTML element, or something that looks more like a dialog (but isn't). That would give you the control you need. All of the major JavaScript frameworks (YUI, Dojo, and others) would give you the ability to display a message asynchronously.

Solution 5:

Not reliably. Use a div on the new page.