|
Part2 - onbeforeunload Event In this tutorial, we are going to use onbeforeunload event to let the user know the progress of loading next page. onbeforeunloadonbeforeunload is an event that fires before the unload event when the page is unloaded. It is not a public standard. It is introduced by Internet Explorer 4. However, other browser realized the importance of the event for web application. Most modern browsers now support this event. DependenciesThe major dependency is ModalBox. You can use other modal popup library if you want. ModalBox is chosen because it is merely a function call to show the modal popup. It depends on prototype.js and scriptaculous. They are included in the ModalBox download package. So, all you need is the following code: <link rel="stylesheet" href="/css/modalbox.css" type="text/css" media="screen" /> <script type="text/javascript" src="/js/prototype.js"></script> <script type="text/javascript" src="/js/scriptaculous.js?load=effects"></script> <script type="text/javascript" src="/js/modalbox.js"></script> ImplementationThe implementation consists of
ScriptThe script is simple. We only write the function to show the modal popup which will include in an iframe element. Line 1 to 14 can be put in a common source file (e.g. checkprogress.jsp, checkprogress.php) so you can include them and reuse them. Line 13 to 16 will be the configuration of
Please also note that checkprogress is actually is calling an anonymous function with window.setTimeout(). That means the modal popup will not show if the next page is loaded with one second (line 11). process.phpThis PHP page can be anything. This example only sleep for 20 seconds and redirect to the Referer header. In actual usage will be updating the progress in the loop. <? $n=20; for ($i = 0; $i < $n; $i++) { sleep(1); } header("Location: ".$_SERVER['HTTP_REFERER']); ?> progress.phpThis PHP will read the progress updated by process.php. This example shows the server time. Since it is hosted in a iframe, the background color is set to bransparent. It also set the meta refresh to update the page automatically. <html> <head> <meta http-equiv="refresh" content="1"> </head> <body style="background-color:transparent"> <? echo date("r"); ?> </body> </html> DemoClick here to see the effect. NoteThere was an attempt to implement the same function with AJAX (i.e. client-side refresh) without an iframe (i.e. server-side refresh):
However, this does not work quite well because of cross-browser issue. Mozilla browsers (e.g. FireFox, SeaMonkey) and Internet Explorer can start an AJAX request when the page is being unload. Google Chrome, however, is still running the script but it does not invoke the AJAX request. So, it does not work with Chrome. I think it is the behavior of WebKit browsers (e.g. Safari). If you only target for FireFox and IE, you can use the code above and a more simple progress.php (i.e. you can remove all the HTML tags). |