Darryl
Darryl

Reputation: 21

jQuery beforeunload

I have been rewriting a classic asp Shopping basket using jQuery and Ajax. My problem is i want a logout functionality when the user closes the browser. this works but if i refresh the page the code to logout fires and i lose everything. I found this jquery code piece:

    var inFormOrLink = false;
    $('a').live('click', function () { inFormOrLink = true; });
    $('form').bind('submit', function () { inFormOrLink = true; });
    $(window).bind("beforeunload", function () {
        if (inFormOrLink == false) {
            $.post("logout.asp");
        }

    })

Does anyone have any ideas how i can perform logout.asp ONLY when the user closes the browser OR indeed changes to a new website. But keep the information while in the Application (in IIS).

Upvotes: 2

Views: 613

Answers (1)

alex
alex

Reputation: 490263

The beforeunload is fired when you leave/refresh the current page, and you can only successfully run code that blocks the browsing session, such as alert() or prompt().

Upvotes: 1

Related Questions