0

I am currently building a simple ajax interface for a booking and tracking from for use on the web.

The problem I have is that if the user hits the back button or the refresh button any unsaved data is erased and the page returns to its normal starting position.

I have implemented the basic javascript onUnload function as below:

function winClose()
{
    if (confirm("Are you sure you want to navigate away from this page?"))
    {
         window.close();
    }

    return false;
}

along with the addition of:

<body onUnload="return winClose(); return false;">

This gives the rather clumsy alert box that no-one really likes but it still resets the page to its original starting position.

Does anyone know of a way to implement something similar that does not reset the page and just ignores everything if the user chooses not to navigate away from the page ?

Sideshow
  • 1,321
  • 6
  • 28
  • 50
  • Have you tried to save your needed data with localstorage or cookies? – Oliver Jun 04 '12 at 10:45
  • This is looking like the correct path to follow - I was just hoping that there might be a way to over-rule the return element on the javascript confirm box. – Sideshow Jun 04 '12 at 12:49

1 Answers1

0

You'd want to store your position (either as an integer or as a JSON array) and the data involved in a cookie (or cookies) on the users browser, as you go along. You can't always rely on onbeforeunload for this.

A good jQuery cookie plugin is this: https://github.com/carhartl/jquery-cookie

Death
  • 1,999
  • 12
  • 14