Autoclose alert

Okky picture Okky · Sep 26, 2013 · Viewed 32.6k times · Source

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

Answer

NaYaN picture NaYaN · Sep 26, 2013

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);