6

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.

4

1 Answer 1

10

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

$(window).bind('beforeunload', function(){ return 'DataTest'; });​ 

JsFiddle Demo

Sign up to request clarification or add additional context in comments.

5 Comments

It's funny, the JsFiddle demo works fine in my chrome browser, but if I implement on my page, it does nothing.
@Nicolai - check might be you fissing jquery file reference in page ...it will work for sure..
Yea, I have a ton of jquery on the page already, that works. It's weird.
Yea, unfortunately this didn't do anything for me. It works in IE, but for some reason not in chrome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.