Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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?

like image 606
Darryl Hein Avatar asked Nov 19 '08 22:11

Darryl Hein


People also ask

Does JavaScript alert stop execution?

One of the nice things about the built-in JavaScript alert is that - unlike virtually anything else in JavaScript - it's synchronous. It's completely blocking, and no other code will execute until it's been dismissed.

Can we use alert in JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

What we can use instead of alert in JavaScript?

Add hidden div aligned with your elements and show the message on hidden div's instead of alert box.


1 Answers

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.

like image 182
Aaron Powell Avatar answered Sep 28 '22 18:09

Aaron Powell