Nicolai
Nicolai

Reputation: 2915

Detect when user navigates away from page

How do I detect that a user is navigating away from my page? Kind of like stackoverflow does, if you have started writing a post.

I have tried $(window).unload() in jQuery, but I can't get it to work.

This statement is not entirely true, in IE9 it works, in fact a bit too well. It also pops up, if the page is refreshed. But in Chrome, nothing triggers.

Upvotes: 6

Views: 7158

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176956

Include the jQuery library in your code, and then try out this

$(window).bind('beforeunload', function(){

        return 'DataTest';
});​

JsFiddle Demo

Upvotes: 10

Related Questions