Search code examples
jqueryonbeforeunloadonunload

how to keep page position in ajax/jquery interface


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 ?


Solution

  • 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