views:

650

answers:

5

I'm looking for something like alert() but doesn't "puase" 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?

+8  A: 

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.

Slace
Don't pass a string to setTimeout, pass a function. setTimeout(function() { alert('hello world'); } ) is much nicer -- functions are first degree objects in Javascript
Gareth
Yeah I know I should have done it as a function rather than a string to be eval'ed.
Slace
+1  A: 

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.

David M. Karr
A: 

Not reliably. Use a div on the new page.

Mark
Need it before the page so users know their data is being submitted.
Darryl Hein
Then the best way would be to submit it in the background to a web service that returns updates. That way you don't need to change page until it's complete. setTimeout("alert()") is often blocked: it depends on your situation whether that is likely to be a problem.
Mark
A: 

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.

Jon DellOro
Alright idea, but not very visible. I'm trying to stop the user from clicking the link again, so they don't end up submitting the form multiple times (won't screw up the data, but will it will mess with the user's head as they aren't expecting the form to submit, but need to save the data).
Darryl Hein
+2  A: 

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

eimaj
Yeah, used this before, but I don't want to add all the extra code for something that should be simple.
Darryl Hein
This is the obligatory jQuery response - whenever a JavaScript question is asked, the obligatory jQuery response is sure to be found (at least 95% of the time). :P
Jason Bunting