49

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?

8 Answers 8

56

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
Yeah I know I should have done it as a function rather than a string to be eval'ed.
This solution doesn't work for Chrome, the alert will pause all browser script even in setTimeout.
setTimeout is not asynchronous, it just postpones the synchronous execution of blocking code. Once the alert is displayed, code execution is still blocked until the user clicks OK.
Yeah, what John Weisz said, it doesn't help, as soon as the alert is there the page freezes in the background (except for some animation)
|
4

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

1 Comment

Yeah, used this before, but I don't want to add all the extra code for something that should be simple.
4

98% of the time there is a way to move your message script so that it calls after everything else executes. For that other 2%, I like to use floating divs that look something like this. You can then change your CSS to match the style of your application/website or to make it look more like a standard windows popup.

/*HTML*/
<div class="floatingDiv" id="msgBox" style="visibility:hidden"></div>

/*javaScript*/
function openWindow(id){
"use strict";
document.getElementById(id).style.visibility = 'visible'; 
}
function closeWindow(id){
"use strict";
document.getElementById(id).style.visibility = 'hidden'; 
}
function myMsgBox(TITLE,MESSAGE) {
"use strict";   
document.getElementById("msgBox").innerHTML = "<a href=\"javascript:closeWindow('msgBox')\" style=\"float:right\"><img src=\"imgs/close.png\" onmouseover=\"src='imgs/closeOver.png'\" onmouseout=\"src='imgs/close.png'\"/ alt=\"[close]\"></a><h2 style=\"text-align:center; margin-top:0px;\">" + TITLE + "</h2><hr><p align=\"left\">" + MESSAGE + "</p>";
openWindow("msgBox");
}

/*CSS*/
.floatingDiv {
position:absolute;  
z-index:10000;
left:33%;
top:250px;
width:33%;
background-color:#FFF;
min-width:217px;
text-align: left;
border-radius: 10px 10px;
border:solid;
border-width:1px;
border-color:#000;
vertical-align:top;
padding:10px;

background-image: -ms-linear-gradient(top, #CCCCCC 0%, #FFFFFF 25px, #FFFFFF 100%);
background-image: -moz-linear-gradient(top, #CCCCCC 0%, #FFFFFF 25px, #FFFFFF 100%);
background-image: -o-linear-gradient(top, #CCCCCC 0%, #FFFFFF 25px, #FFFFFF 100%);
background-image: -webkit-linear-gradient(top, #CCCCCC 0%, #FFFFFF 25px, #FFFFFF 100%);
background-image: linear-gradient(to bottom, #CCCCCC 0%, #FFFFFF 25px, #FFFFFF 100%);

box-shadow:3px 3px 5px #003; 
filter: progid:DXImageTransform.Microsoft.Shadow(color='#000033', Direction=145, Strength=3);
}

Comments

2

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.

3 Comments

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).
what is changing window.status supposed to do?
@maxpleaner Right now, nothing. It was more useful when browsers had permanent status bars.
1

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.

Comments

1

Not reliably. Use a div on the new page.

2 Comments

Need it before the page so users know their data is being submitted.
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.
1

Years later, there is now dialog.show() (as opposed to dialog.showModal()) that gives you a non-modal alert-lookalike.

Comments

0
<a-toast style="visibility:hidden"></a-toast>

<script>
    function toast(message = "", options = { duration: 2000 }) {
        const el = document.querySelector("a-toast")

        function remove() {
            el.style.visibility = 'hidden';
        }

        if (!message) {
            remove()
            return
        }
        el.style.visibility = 'visible';
        // el.innerText = message
        el.innerHTML = message

        // Hide
        if (options.duration) {
            setTimeout(() => {
                remove()
            }, options.duration)
        }
    }

// Usage
    toast("Hi <b>there</b>", { duration: 0 })
</script>

<style>
    a-toast {
        position: fixed;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 10000;
        bottom: 10px;
        background-color: #FFF;
        min-width: 200px;
        text-align: left;
        border: solid;
        border-width: 1px;
        border-color: #000;
        vertical-align: top;
        padding: 10px;
    }
</style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.