onUnload event not working

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']; ?>">
Who is Participating?
Michel PlungjanIT ExpertCommented:
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
ErikalAuthor Commented:
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?
ErikalAuthor Commented:
Thank you.
I research that link.
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.