最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Autoclose alert - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Sep 26, 2013 at 6:24 OkkyOkky 10.5k15 gold badges77 silver badges123 bronze badges 7
  • 1 I don't think that's possible; never seen it in 6 years of coding. – Joe Simmons Commented Sep 26, 2013 at 6:27
  • @sreeekesh You can not close an alert box, just you can hide it . – HybrisHelp Commented Sep 26, 2013 at 6:27
  • @ankit337: Hide it how? – Joe Simmons Commented Sep 26, 2013 at 6:28
  • 1 stackoverflow.com/questions/15466802/… – Nikolay Talanov Commented Sep 26, 2013 at 6:29
  • Thanx guys. I think I will go with jQuery dialogue then :) – Okky Commented Sep 26, 2013 at 6:31
 |  Show 2 more comments

5 Answers 5

Reset to default 19

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

You can't close alert any how .

But you can use div To show your alert MSG.

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

You can use as :

Mymsg('close',2000)

jsFiddle Demo

Basically you can imitate an alert box with a popup window and close it whenever you want, call the following function:

function myAlert(msg,title,width,height,timeout) {
    var myWindow = window.open("", "",`width=${width},height=${height},left=${(window.outerWidth/2-width/2)},top=0`); //open a new popup at the top height and middle width of the current window
    myWindow.document.write(`<center id="msg">`+msg+`</center>`); //write the message you want to display
    myWindow.document.title = title; //write the title of the alert box
    setTimeout(function(){
        myWindow.close(); //close the popup
    },timeout||3000) //in 3 secondes (3000 milliseconds)
}

by the way if you want to close the alert on call, just use define the variable "myWindow" earlier (out of myAlert() function) as a global variable and then call myWindow.close(); once you already called myAlert() and remove

setTimeout(function(){
    myWindow.close(); //close the popup
},timeout||3000) //in 3 secondes (3000 milliseconds)

from the function myAlert()

I updated the style settings, so it shows like a splash screen in the center of your page, and centers the text inside it.

Call it like this:

alertTimeout("System Message<br>This is a test message<br>This alert will auto-close",5000)

The function:

function alertTimeout(mymsg,mymsecs)
{
 var myelement = document.createElement("div");
myelement.setAttribute("style","background-color: grey;color:black; width: 450px;height: 200px;position: absolute;top:0;bottom:0;left:0;right:0;margin:auto;border: 4px solid black;font-family:arial;font-size:25px;font-weight:bold;display: flex; align-items: center; justify-content: center; text-align: center;");
 myelement.innerHTML = mymsg;
 setTimeout(function(){
  myelement.parentNode.removeChild(myelement);
 },mymsecs);
 document.body.appendChild(myelement);
}

This is another solution

window.setTimeout('alert("Error Found ");window.close();', 5000);

发布评论

评论列表(0)

  1. 暂无评论