Alert Box As Soon As HTML Page Loads

One way to display a dialog box as soon as an HTML page finishes loading is to place some Javascript code right before the </body> tag. However, this might cause the browser's progress bar to make it seem like the page is still loading, which goes on until the user dismisses the dialog box.

The solution is to use setTimout, as follows:

HTML:
  1. <script type="text/javascript">
  2. setTimeout("alert('...')", 10);
  3. </script>

18 September 2007 | Software engineering, Javascript | Comments

2 Responses to “Alert Box As Soon As HTML Page Loads”

  1. 1 Ady 24 September 2007 @ 10:04 am

    That’s interesting. Any scenario examples why we want it to give an alert after the page finished loading instead of before (or onload event)? I can’t think of any, seriously… maybe because none of my web jobs so far requires this.

  2. 2 Rizal 26 September 2007 @ 11:33 am

    Between using the body onload event and inserting a script snippet before the </body> tag - personal preference I suppose. I like to do the latter for PHP pages when I want to display a dialog box only under certain circumstances. I find it easier to read my spaghetti code somehow …

Comments:

  1.  
  2.  
  3.