2

I want to show alert box only for 3 seconds (Means it show & hide automatically). I have search lot of in google but doesn't get answer. Any suggestion really appreciated.

I have done with new window but still finding solution with alert box. Chk My code :

function fn() {
    var w = window.open('', '', 'width=300,height=2px')
    w.document.write('Product has been added to your Order List !')
    w.focus()
    setTimeout(function () { w.close(); }, 2000);
}
Steve
  • 101
  • 1
  • 1
  • 7
  • This isn't a code request forum. Please try something and come back with a specific problem you are facing with your solution. – webnoob Aug 21 '13 at 09:16
  • 2
    The only way to hide an `alert()` box is by user interaction. – George Aug 21 '13 at 09:16
  • The user has to manually close an alert box by clicking ok. If you need to show alert and fade away, use jquery or simple solution is to use a div as shown in this post `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); }` Call this function from code behind or from the click of any button. – Nisha Aug 21 '13 at 09:28
  • Thanks all for your view ! – Steve Aug 21 '13 at 09:29
  • I didn't vote for off-topic, this question is not off topic. But it is a duplicate of lot of other questions here. – eis Aug 21 '13 at 09:33

3 Answers3

5

You cannot hide or cancel an alert() box automatically from JavaScript.

ComFreek
  • 29,044
  • 18
  • 104
  • 156
5
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",5000);
user3265137
  • 67
  • 1
  • 4
1

I'm assuming that you would like to control all this using javascript. With native alert boxes (the ones using alert()), you cannot do this.

What you could do is to use an alternative such as JQuery modal dialog. It is generated from javascript, so you would have a possibility of closing it from javascript as well. You could use setTimeout() to activate 3 second timeout, and $('#your-dialog-id').dialog( "close" ); to close the modal dialog.

eis
  • 51,991
  • 13
  • 150
  • 199