user198989
user198989

Reputation: 4665

How to catch if user clicks back button?

I am using this snippet, it works in all actions except if user clicks back button to leave the page.

$(window).bind('hashchange', function () 
{
    $.post("track.php", 
    {
        ip: ip,
        referer: referer,
    });
});

$(window).bind('beforeunload', function () 
{
    $.post("track.php", 
    {
        ip: ip,
        referer: referer,
    });
});

$(window).unload(function () 
{
    $.post("track.php", 
    {
        ip: ip,
        referer: referer,
    });
});

Is it possible to catch if user clicks back button ?

Upvotes: 0

Views: 181

Answers (1)

Brad M
Brad M

Reputation: 7898

Make your ajax request synchronous. This doesn't work in opera last I checked though.

$.ajax({
    async: false
});

Upvotes: 3

Related Questions