How can I create a popup message window that will close itself after 'n' seconds?

Jorge Jordão

None of the predefined dialogs (alert, confirm, prompt) allow you to do that.

However, you can create your custom dialog window with window.open, and using setTimeout(function, msecs) you can close it after a specified time.

Example:

MyDialog.html (HTML content for the dialog window)


<HTML>
  <HEAD>
    <TITLE>This is my custom dialog</TITLE>
  </HEAD>
  <BODY BGCOLOR="#C0C0C0">
    <CENTER>
    <FONT FACE="Arial" SIZE="2">
      This is my custom dialog message</FONT>
    <FORM>
      <INPUT TYPE="button" VALUE="    OK    "  
        ONCLICK="window.close()">
    </FORM>
  </BODY>
</HTML>

Test.html (page that opens a dialog and closes it after a specified time)


<HTML>
  <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
      function closeDialog() {
        dialog.close();
      }                        
      var dialog = window.open("custom2.html","mywindow",
        "resizable=no,menubar=no,width=200,height=100");
      setTimeout('closeDialog()',5000);
    </SCRIPT>
  </HEAD>
</HTML>
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact