javascriptjqueryalertauto-close

Autoclose alert


Is there any way to close a javascript alert() automatically?

I have an alert

alert("Error found");

I want to close it after a few second. Is that possible or shall I go for jQuery dialogue


Solution

  • jsFiddle Demo

    This functionality is not possible with an alert. However, you could use a div

    function tempAlert(msg,duration)
    {
     var el = document.createElement("div");
     el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:white;");
     el.innerHTML = msg;
     setTimeout(function(){
      el.parentNode.removeChild(el);
     },duration);
     document.body.appendChild(el);
    }
    

    Use this like this:

    tempAlert("close",1000);