onUnload event not working

Posted on 2008-11-05
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
    LVL 75

    Expert Comment

    by:Michel Plungjan
    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

    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


    Author Closing Comment

    Thank you.
    I research that link.
    LVL 17

    Expert Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    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…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    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…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now