[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


onUnload event not working

Posted on 2008-11-05
Medium Priority
Last Modified: 2012-05-05
I have a test page where there is a 15 minue time limit.
I also have an unOnload event which should submit a hidden form field to record the time  emaining if the user navigates away from the page or refreshes.

The unOnload event does not seem to submit the form as the form always passes the value zero.

Can any one help.
I call ther unOnload event in the body tag.
Here is the relevent part of ther javascript and the form:

<title>English Test</title>

<script type="text/javascript">
var timeLeft = <?=$timeLeft ?>;
var tId = "";
//var leave = "";
function countdown() {
       timeLeft -= 1000;
      if (timeLeft<=0){
      } else {

window.onload=function() {
  tId = setInterval('countdown()',1000);
function updateTimer() {
   var timerArea = document.getElementById('timer');
   var remArea = document.getElementById('eng1time');
   var numSeconds = timeLeft / 1000;
   var displaySeconds = numSeconds % 60;
   var displayMinutes = (numSeconds - displaySeconds) / 60;

   // format display a little more by putting a leading 0 for small seconds
   if (displaySeconds < 10) {
       displaySeconds = "0" + displaySeconds;

      timerArea.value = displayMinutes + ":" + displaySeconds;
      remArea.value = timeLeft;
      function nextPage(){
            //assuming you have <form id="English1"...>
            var f = document.getElementById("English1");
            for( var i=0; i < f.elements.length; ++i)
                    if( String(f.elements[i].type).toLowerCase() != "submit" )

            alert('Time allowed for the English test has expired! Please press the Maths Test button to continue');

       function activate(f){
  if (timeLeft > 100000 && confirm('Are you sure you want to leave the English test? You still have some time left to go back and check your answers. If you are sure you want to go on to the Maths Test, please click OK button. Otherwise, click Cancel button to return to the English test and use the remaining minutes to improve your English score.'))
return false;
            for( var i=0; i < f.elements.length; ++i)
      return true;


<body text="#000000" style="background: #e5e5e5; text-align:center; height:2500px;" ONUNLOAD="window.document.getElementById('form1').submit()">
<form id="form1" method="GET" action="<? echo $_SERVER['PHP_SELF']; ?>">
Question by:Erikal
  • 2
  • 2
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22892966
You cannot rely on the onUnload to have access to anything on the page

You can use onBeforeUnload on some browsers or continuously set cookies and onUnload load a page that looks at the cookies

Author Comment

ID: 22893710
I susspected that onUnload event was not a very good idea.
So how would I be able to trigger the form to pass its single hidden field variable value to a php session variable when the user navigates away from the page? Is there any way other than using cookies?
LVL 75

Accepted Solution

Michel Plungjan earned 2000 total points
ID: 22893746

Author Closing Comment

ID: 31513541
Thank you.
I research that link.
LVL 17

Expert Comment

ID: 22893924
Hi Erikal, I have been following this question to, as I am interested in the solution. Before I suggested you try asking the question in a different zone, I tried using onBeforeUnload, but I am not good enough at javascript to figure how how to get the current time into the database in a javascript event, so can you show what worked for you?

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question