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

后端 未结 6 1107
甜味超标
甜味超标 2020-11-28 07:51

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 sub

相关标签:
6条回答
  • 2020-11-28 08:01

    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);
    }
    
    0 讨论(0)
  • 2020-11-28 08:07

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

    0 讨论(0)
  • 2020-11-28 08:13

    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.

    0 讨论(0)
  • 2020-11-28 08:17

    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.

    0 讨论(0)
  • 2020-11-28 08:18

    Not reliably. Use a div on the new page.

    0 讨论(0)
  • 2020-11-28 08:19

    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.

    0 讨论(0)
提交回复
热议问题