admin管理员组

文章数量:1289845

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.

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.

Share Improve this question edited Nov 29, 2012 at 13:35 Nicolai asked Nov 29, 2012 at 13:22 NicolaiNicolai 2,9158 gold badges45 silver badges55 bronze badges 4
  • look at this: stackoverflow./questions/3032249/… – silly Commented Nov 29, 2012 at 13:24
  • 1 Also here: kbeezie./cross-browser-exit-pop – beyond-code Commented Nov 29, 2012 at 13:25
  • @Roysvork, that seems to work for me! Care you to submit an answer? – Nicolai Commented Nov 29, 2012 at 13:44
  • 1 There is an excellent article on the matter here: kbeezie./cross-browser-exit-pop This explains all the ins and outs of the different browsers. Enjoy! – beyond-code Commented Nov 29, 2012 at 14:11
Add a ment  | 

1 Answer 1

Reset to default 10

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

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

        return 'DataTest';
});​

JsFiddle Demo

本文标签: javascriptDetect when user navigates away from pageStack Overflow